jsonb

Extract key, value from json objects in Postgres

无人久伴 提交于 2019-12-04 17:48:12
问题 I have a Postgres table that has content similar to this: id | data 1 | {"a":"4", "b":"5"} 2 | {"a":"6", "b":"7"} 3 | {"a":"8", "b":"9"} The first column is an integer and the second is a json column. I want to be able to expand out the keys and values from the json so the result looks like this: id | key | value 1 | a | 4 1 | b | 5 2 | a | 6 2 | b | 7 3 | a | 8 3 | b | 9 Can this be achieved in Postgres SQL? What I've tried Given that the original table can be simulated as such: select *

jsonb existential operators with parameterised queries

。_饼干妹妹 提交于 2019-12-04 17:24:50
问题 ...or the question mark question. I am currently implementing the search functionality for a postgres database, in php, that uses the new jsonb type. To achieve this I am executing prepared statements with named placeholders. However I have run into an interesting problem whilst trying to use some of the new postgres JSON containment and existence operators along with named placeholders. The basis of issue being that the operators themselves use the question mark ? as part of their syntax. i

Join tables using a value inside a JSONB column

一曲冷凌霜 提交于 2019-12-04 17:18:11
There are two tables: Authorized Contacts ( auth_contacts ): ( userid varchar contacts jsonb ) contacts contains an array of contacts with attributes {contact_id, type} discussion : ( contact_id varchar discussion_id varchar discussion_details jsonb ) The table auth_contacts has at least 100k records making it non JSONB type is not appropriate according as it would double or triple the amount of records. Sample data for auth_contacts : userid | contacts '11111' | '{"contact": [{"type": "type_a", "contact_id": "1-A-12"} , {"type": "type_b", "contact_id": "1-A-13"}]}' discussion table has 5

Looking for a right EAV structure based on jsonb

别来无恙 提交于 2019-12-04 12:31:46
问题 I wondering what will be the right approach to build EAV on jsonb. I have Attribute -> Values tables as like in standard EAV. CREATE TABLE attribute_values ( id INTEGER, attribute_id INTEGER, value VARCHAR(255) ); CREATE TABLE attributes ( id INTEGER, name VARCHAR(255) ); Values will saved in attributes filed of Entity CREATE TABLE entity ( id INTEGER, title TEXT, attributes JSONB ); Tables Attribute created to control duplicate attributes their types and better determine what it's a

recursively flatten a nested jsonb in postgres without unknown depth and unknown key fields

最后都变了- 提交于 2019-12-04 11:49:20
How can I recursively flatten a nested jsonb in postgres which I don't know the depth and the field at each depth? (see example below) A postgressql query to do the flattening would be much apreciated { "xx": "", "xx": "", "form": "xxx", "type": "", "content_type": "xxx", "reported_date": , "contact": { "imported_date": "", "name": "", "phone": "", "alternate_phone": "", "specialization": "", "type": "", "reported_date": , "parent": { "_id": "xxx", "_rev": "xxx", "parent": "", "type": "xxx" } } } I have searched in stack overflow but they only consider jsonb's which have a single depth and the

Is it possible to use Hibernate with PostgreSql's JSONB data type?

妖精的绣舞 提交于 2019-12-04 10:41:51
问题 Hibernate 5 does not support the PostgreSQL jsonb data type by default. Is there any way to implement jsonb support for Hibernate + Spring JPA? If there is a way, what are the pros and cons of using jsonb with Hibernate? 回答1: Thanks Vlad Mihalcea we have such opportunity! ) He created hibernate-types lib: <dependency> <groupId>com.vladmihalcea</groupId> <artifactId>hibernate-types-52</artifactId> <version>2.1.1</version> </dependency> which adds a support of 'json', 'jsonb' and other types to

add index on jsonb field

旧时模样 提交于 2019-12-04 09:14:19
问题 I need to search over the values of an array key of jsonb field in Postgres. field: {'array_key' : [1, 2, 3, 4]} Is it possible to add index on array_key or is there any optimized method to search over the values ? search query will be something like select * where field['array_key'].include?(2) 回答1: You can create index on jsonb keys as, add_index :table_name, :field, :using => :gin, :expression => "(field->'array_key')", :name => 'index_table_name_on_field_array_keys' Then, you can search

Postgresql query for objects in nested JSONB field

不羁的心 提交于 2019-12-04 09:07:08
问题 I am using PostgreSQL 9.6, and I have a table named "ItemDbModel" with two columns looks like: No integer, Content jsonb Say I put many records like: "No": 2, {"obj":"x","Item": {"Name": "BigDog", "Model": "NamedHusky", "Spec":"red dog"}} "No": 4, {"obj":"x","Item": {"Name": "MidDog", "Model": "NamedPeppy", "Spec":"no hair"}} "No": 5, {"obj":"x","Item": {"Name": "BigCat", "Model": "TomCat", "Spec":"blue color"}} How can I query the table for: Records where "Content.Item.Name" contains "Dog"

How to select only part of json, stored in Postgres, with ActiveRecord

纵饮孤独 提交于 2019-12-04 06:33:01
Say I have a model User , which has a field of type json called settings . Let's assume that this field looks roughly like this: { color: 'red', language: 'English', subitems: { item1: true, item2: 43, item3: ['foo', 'bar', 'baz'] } } If I do User.select(:settings) I will get all the settings for each user. But I want to get only the languages for a user. I tried both: User.select("settings -> 'language'") and User.select("settings ->> 'language'") but this just returns empty objects: [#<User:0x007f381fa92208 id: nil>, ...] Is this at all possible? If yes - can I do it using just json or do I

how do I perform a case-insensitive search in a Postgres 9.4 JSONB column?

强颜欢笑 提交于 2019-12-03 16:29:16
i'm using this query to look for data in a table where profile is a JSONB column and it works but only if the name is exactly that SELECT * FROM "users" WHERE "profile" @> '{"name":"Super User"}' is it possible to have more flexibility like case insensitivity, wildcards and so on ? Something like "Super%" or "super user" I found the solution to my problem: SELECT * FROM "users" WHERE (profile #>> '{name}') ILIKE 'super %' I don't know if this is performing well enough but it works. Probably it's wise to add an index to it. 来源: https://stackoverflow.com/questions/27681163/how-do-i-perform-a