jsonb

postgresql 9.5 using jsonb_set for updating specific jsonb array value

自古美人都是妖i 提交于 2019-11-27 23:47:05
Currently I am working with postgreSQL 9.5 and try to update a value inside an array of a jsonb field. But I am unable to get the index of the selected value My table just looks like: CREATE TABLE samples ( id serial, sample jsonb ); My JSON looks like: {"result": [ {"8410": "ABNDAT", "8411": "Abnahmedatum"}, {"8410": "ABNZIT", "8411": "Abnahmezeit"}, {"8410": "FERR_R", "8411": "Ferritin"} ]} My SELECT statement to get the correct value works: SELECT id, value FROM samples s, jsonb_array_elements(s.sample#>'{result}') r WHERE s.id = 26 and r->>'8410' = 'FERR_R'; results in: id | value --------

jsonb vs jsonb[] for multiple addresses for a customer

你离开我真会死。 提交于 2019-11-27 16:19:29
It's a good idea to save multiple addresses in a jsonb field in PostgreSQL. I'm new in nosql and I'd like to test PostgreSQL to do that. I don't want to have another table with addresses, I prefer to have it in the same table. But I'm in doubt, I've seen PostreSQL have jsonb and jsonb[]. Which one is better to store multiple addresses? If I use jsonb, I think I must to add a prefix for every field like this: "1_adresse_line-1" "1_adresse_line-2" "1_postalcode" "2_adresse_line-1" "2_adresse_line-2" "2_postalcode" "3_adresse_line-1" "3_adresse_line-2" "3_postalcode" etc. Is it better to use

Merging JSONB values in PostgreSQL?

痴心易碎 提交于 2019-11-27 16:06:08
Using the || operator yields the following result: select '{"a":{"b":2}}'::jsonb || '{"a":{"c":3}}'::jsonb ; ?column? ----------------- {"a": {"c": 3}} (1 row) I would like to be able to do achieve the following result ( ?? just a placeholder for the operator): select '{"a":{"b":2}}'::jsonb ?? '{"a":{"c":3}}'::jsonb ; ?column? ----------------- {"a": {"b": 2, "c": 3}} (1 row) So, you can see the top-level a key has its child values "merged" such that the result contains both b and c . How do you "deep" merge two JSONB values in Postgres? Is this possible, if so how? A more complex test case:

PostgreSQL jsonb, `?` and JDBC

左心房为你撑大大i 提交于 2019-11-27 14:52:14
I am using PostgreSQL 9.4 and the awesome JSONB field type. I am trying to query against a field in a document. The following works in the psql CLI SELECT id FROM program WHERE document -> 'dept' ? 'CS' When I try to run the same query via my Scala app, I'm getting the error below. I'm using Play framework and Anorm, so the query looks like this SQL(s"SELECT id FROM program WHERE document -> 'dept' ? {dept}") .on('dept -> "CS") .... SQLException: : No value specified for parameter 5. (SimpleParameterList.java:223) (in my actual queries there are more parameters) I can get around this by

Merging Concatenating JSON(B) columns in query

旧城冷巷雨未停 提交于 2019-11-27 12:44:19
问题 Using Postgres 9.4, I am looking for a way to merge two (or more) json or jsonb columns in a query. Consider the following table as an example: id | json1 | json2 ---------------------------------------- 1 | {'a':'b'} | {'c':'d'} 2 | {'a1':'b2'} | {'f':{'g' : 'h'}} Is it possible to have the query return the following: id | json ---------------------------------------- 1 | {'a':'b', 'c':'d'} 2 | {'a1':'b2', 'f':{'g' : 'h'}} Unfortunately, I can't define a function as described here. Is this

Postgresql query array of objects in JSONB field

两盒软妹~` 提交于 2019-11-27 11:22:13
I have a table in a postgresql 9.4 database with a jsonb field called receivers. Some example rows: [{"id": "145119603", "name": "145119603", "type": 2}] [{"id": "1884595530", "name": "1884595530", "type": 1}] [{"id": "363058213", "name": "363058213", "type": 1}] [{"id": "1427965764", "name": "1427965764", "type": 1}] [{"id": "193623800", "name": "193623800", "type": 0}, {"id": "419955814", "name": "419955814", "type": 0}] [{"id": "624635532", "name": "624635532", "type": 0}, {"id": "1884595530", "name": "1884595530", "type": 1}] [{"id": "791712670", "name": "791712670", "type": 0}] [{"id":

Create timestamp index from JSON on PostgreSQL

浪子不回头ぞ 提交于 2019-11-27 07:13:42
问题 I have a table on PostgreSQL with a field named data that is jsonb with a lot of objects, I want to make an index to speed up the queries. I'm using few rows to test the data (just 15 rows) but I don't want to have problems with the queries in the future. I'm getting data from the Twitter API, so with a week I get around 10gb of data. If I make the normal index CREATE INDEX ON tweet((data->>'created_at')); I get a text index, if I make: Create index on tweet((CAST(data->>'created_at' AS

Upgrade PostgreSQL JSON column to JSONB?

大城市里の小女人 提交于 2019-11-27 05:48:21
问题 After upgrading to PostgreSQL 9.4, how do I convert all my JSON columns into JSONB columns? I don't mind losing any duplicate keys and whitespace. 回答1: ALTER TABLE t ALTER COLUMN j TYPE jsonb USING j::text::jsonb; 回答2: In the context of Rails, here is an ActiveRecord migration alternative: def change reversible do |dir| dir.up { change_column :models, :attribute, 'jsonb USING CAST(attribute AS jsonb)' } dir.down { change_column :models, :attribute, 'json USING CAST(attribute AS json)' } end

PostgreSQL rename attribute in jsonb field

风格不统一 提交于 2019-11-27 02:40:06
问题 In postgresql 9.5, is there a way to rename an attribute in a jsonb field? For example: { "nme" : "test" } should be renamed to { "name" : "test"} 回答1: In UPDATE use delete (-) and concatenate (||) operators, e.g.: create table example(id int primary key, js jsonb); insert into example values (1, '{"nme": "test"}'), (2, '{"nme": "second test"}'); update example set js = js - 'nme' || jsonb_build_object('name', js->'nme') where js ? 'nme' returning *; id | js ----+------------------------- 1 |

jsonb query with nested objects in an array

百般思念 提交于 2019-11-27 02:20:44
问题 I'm using PostgreSQL 9.4 with a table teams containing a jsonb column named json . I am looking for a query where I can get all teams which have the Players 3 , 4 and 7 in their array of players. The table contains two rows with the following json data: First row: { "id": 1, "name": "foobar", "members": { "coach": { "id": 1, "name": "A dude" }, "players": [ { "id": 2, "name": "B dude" }, { "id": 3, "name": "C dude" }, { "id": 4, "name": "D dude" }, { "id": 6, "name": "F dude" }, { "id": 7,