jsonb

Appending (pushing) and removing from a JSON array in PostgreSQL 9.2, 9.3, and 9.4?

徘徊边缘 提交于 2019-11-29 02:16:37
For versions greater than 9.5 see this question I have created a table in PostgreSQL using this: CREATE TEMP TABLE jsontesting AS SELECT id, jsondata::jsonb FROM ( VALUES (1, '["abra","value","mango", "apple", "sample"]'), (2, '["japan","china","india", "russia", "australia"]'), (3, '["must", "match"]'), (4, '["abra","value","true", "apple", "sample"]'), (5, '["abra","false","mango", "apple", "sample"]'), (6, '["string","value","mango", "apple", "sample"]'), (7, '["must", "watch"]') ) AS t(id,jsondata); Now what I wanted was to add Something like append_to_json_array takes in the actual

Upgrade PostgreSQL JSON column to JSONB?

▼魔方 西西 提交于 2019-11-29 01:02:08
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. Tometzky ALTER TABLE t ALTER COLUMN j TYPE jsonb USING j::text::jsonb; 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 end I tested this on a table with 120 000 records, each record having four json columns and it took me

How to query jsonb arrays with IN operator

我们两清 提交于 2019-11-28 12:50:11
I'm looking for a way to query postgres jsonb field with kind of "IN" clause inside an array. Let's assume I have a table CREATE TABLE test( id uuid, test_content jsonb, PRIMARY KEY(id) ); INSERT INTO test (id, test_content) VALUES ('aa82a8b8-33ef-4937-bd8c-8a4b40960f18', '[{"label":"a","label1":"1"},{"label":"b","label1":"2"}]'), ('ba82a8b8-33ef-4937-bd8c-8a4b40960f18', '[{"label":"c","label1":"3"}]'), ('da82a8b8-33ef-4937-bd8c-8a4b40960f18', '[{"label":"d","label1":"4"}]'); I need to select rows where label inside test_content's array might be b or d . I tried SELECT * FROM test WHERE test

Create timestamp index from JSON on PostgreSQL

与世无争的帅哥 提交于 2019-11-28 12:44:00
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 timestamp))); I get ERROR: functions in index expression must be marked IMMUTABLE I've tried to make it

Using indexes in json array in PostgreSQL

为君一笑 提交于 2019-11-28 12:37:38
Referring to the original stackoverflow question , I am trying to apply gin indexes to keys in objects of an array in Postgres 9.4 but I'm not getting the results as stated in the first answer. Can you please rectify the error? The steps I followed have been written below. Part 1: Creating table and indexes CREATE TABLE tracks (id serial, artists jsonb); CREATE INDEX tracks_artists_gin_idx ON tracks USING gin (artists); INSERT INTO tracks (id, artists) VALUES (1, '[{"name": "blink-182"}]'); INSERT INTO tracks (id, artists) VALUES (2, '[{"name": "The Dirty Heads"}, {"name": "Louis Richards"}]')

How do I return a jsonb array and array of objects from my data?

北战南征 提交于 2019-11-28 10:08:23
问题 I have the following table: CREATE TABLE mytable ( id serial PRIMARY KEY , employee text UNIQUE NOT NULL , data jsonb ); With the following data: INSERT INTO mytable (employee, data) VALUES ('Jim', '{"sales_tv": [{"value": 10, "yr": "2010", "loc": "us"}, {"value": 5, "yr": "2011", "loc": "europe"}, {"value": 40, "yr": "2012", "loc": "asia"}], "sales_radio": [{"value": 11, "yr": "2010", "loc": "us"}, {"value": 8, "yr": "2011", "loc": "china"}, {"value": 76, "yr": "2012", "loc": "us"}],

Query jsonb column containing array of JSON objects

蓝咒 提交于 2019-11-28 07:39:44
问题 I use PostgreSQL 9.5 and Rails 5. I want to query the jsonb column shown below that holds an array of JSON objects to return all the JSON array element containing {"kind":"person"} and also perform a count. The SQL I use is shown below the json data. Running the query just returns an empty array. I have tried the queries suggested here and here. This is what my jsonb data looks like: '[ {"kind":"person", "filter_term":"56","selected_attr":"customer"}, {"kind":"email", "filter_term":"marketer"

Postgres 9.4 jsonb array as table

百般思念 提交于 2019-11-28 05:12:36
问题 I have a json array with around 1000 elements of the structure "oid: aaa, instance:bbb, value:ccc". {"_id": 37637070 , "data": [{"oid": "11.5.15.1.4", "value": "1", "instance": "1.1.4"} , {"oid": "11.5.15.1.9", "value": "17", "instance": "1.1.4"} , {"oid": "12.5.15.1.5", "value": "0.0.0.0", "instance": "0"}]} oid and instance are unique per json array. If I was given the option to change the structure I would have changed the format to key:value : {"11.5.15.1.4-1.1.4":"1", "11.5.15.1.9-1.1.4"

postgresql migrating JSON to JSONB [duplicate]

我们两清 提交于 2019-11-28 04:19:20
This question already has an answer here: Upgrade PostgreSQL JSON column to JSONB? 2 answers In postgresql 9.4 the new JSONB was incorporated. On a live DB in postgresql 9.3 I have a JSON column. I want to migrate it to JSONB. Assuming I migrated the DB first to 9.4 (using pg_upgrade). What do I do next? Marth ALTER TABLE table_with_json ALTER COLUMN my_json SET DATA TYPE jsonb USING my_json::jsonb; 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

How to escape question mark (?) character with Spring JpaRepository

余生颓废 提交于 2019-11-28 01:16:09
问题 Postgres defines additional jsonb Operators such as ?| . However, using Spring JpaRepository query builder, interrogation character is always considered as a parameter, and I can't figure how to escape it (except inside a single quote string, but then the query is invalid). Example: @Query(value = "SELECT * FROM public.user u WHERE u.authorities ?| array['ROLE_1', 'ROLE_2']", nativeQuery = true) Error: java.lang.IllegalArgumentException: Unable to resolve given parameter name [1] to