jsonb

How do I modify a single property value inside the PostgreSQL JSONB datatype?

早过忘川 提交于 2021-01-22 03:18:33
问题 How do I modify a single field inside the PostgreSQL JSONB datatype? Let's say I have a table called animal like this: id info ------------------------------------------------------------ 49493 {"habit1":"fly","habit2":"dive","location":"SONOMA NARITE"} I'd like to simply change value(say, to upper case or lower case the text) of the location property. so the result, after UPDATE is id info ------------------------------------------------------------ 49493 {"habit1":"fly","habit2":"dive",

How do I modify a single property value inside the PostgreSQL JSONB datatype?

半腔热情 提交于 2021-01-22 03:17:38
问题 How do I modify a single field inside the PostgreSQL JSONB datatype? Let's say I have a table called animal like this: id info ------------------------------------------------------------ 49493 {"habit1":"fly","habit2":"dive","location":"SONOMA NARITE"} I'd like to simply change value(say, to upper case or lower case the text) of the location property. so the result, after UPDATE is id info ------------------------------------------------------------ 49493 {"habit1":"fly","habit2":"dive",

Querying Postgres 9.6 JSONB array of objects

丶灬走出姿态 提交于 2020-12-29 10:16:52
问题 I have the following table: CREATE TABLE trip ( id SERIAL PRIMARY KEY , gps_data_json jsonb NOT NULL ); The JSON in gps_data_json contains an array of of trip objects with the following fields (sample data below): mode timestamp latitude longitude I'm trying to get all rows that contain a certain "mode". SELECT * FROM trip where gps_data_json ->> 'mode' = 'WALK'; I pretty sure I'm using the ->> operator wrong, but I'm unsure who to tell the query that the JSONB field is an array of objects?

Querying Postgres 9.6 JSONB array of objects

ぃ、小莉子 提交于 2020-12-29 10:15:13
问题 I have the following table: CREATE TABLE trip ( id SERIAL PRIMARY KEY , gps_data_json jsonb NOT NULL ); The JSON in gps_data_json contains an array of of trip objects with the following fields (sample data below): mode timestamp latitude longitude I'm trying to get all rows that contain a certain "mode". SELECT * FROM trip where gps_data_json ->> 'mode' = 'WALK'; I pretty sure I'm using the ->> operator wrong, but I'm unsure who to tell the query that the JSONB field is an array of objects?

Querying Postgres 9.6 JSONB array of objects

ぃ、小莉子 提交于 2020-12-29 10:14:50
问题 I have the following table: CREATE TABLE trip ( id SERIAL PRIMARY KEY , gps_data_json jsonb NOT NULL ); The JSON in gps_data_json contains an array of of trip objects with the following fields (sample data below): mode timestamp latitude longitude I'm trying to get all rows that contain a certain "mode". SELECT * FROM trip where gps_data_json ->> 'mode' = 'WALK'; I pretty sure I'm using the ->> operator wrong, but I'm unsure who to tell the query that the JSONB field is an array of objects?

Querying Postgres 9.6 JSONB array of objects

限于喜欢 提交于 2020-12-29 10:14:35
问题 I have the following table: CREATE TABLE trip ( id SERIAL PRIMARY KEY , gps_data_json jsonb NOT NULL ); The JSON in gps_data_json contains an array of of trip objects with the following fields (sample data below): mode timestamp latitude longitude I'm trying to get all rows that contain a certain "mode". SELECT * FROM trip where gps_data_json ->> 'mode' = 'WALK'; I pretty sure I'm using the ->> operator wrong, but I'm unsure who to tell the query that the JSONB field is an array of objects?

Querying Postgres 9.6 JSONB array of objects

寵の児 提交于 2020-12-29 10:14:20
问题 I have the following table: CREATE TABLE trip ( id SERIAL PRIMARY KEY , gps_data_json jsonb NOT NULL ); The JSON in gps_data_json contains an array of of trip objects with the following fields (sample data below): mode timestamp latitude longitude I'm trying to get all rows that contain a certain "mode". SELECT * FROM trip where gps_data_json ->> 'mode' = 'WALK'; I pretty sure I'm using the ->> operator wrong, but I'm unsure who to tell the query that the JSONB field is an array of objects?

Use a Postgres trigger to record the JSON of only the modified fields

我只是一个虾纸丫 提交于 2020-12-15 07:08:40
问题 Is there a way to get the JSON of the only modified fields? Now I use the following trigger but the entire line is printed in the changelog. Example tables: TABLE tbl_changelog ( tbl TEXT, op TEXT, new JSON, old JSON ); TABLE tbl_items ( f1 TEXT, f2 TEXT ); Trigger: CREATE OR REPLACE FUNCTION changelog_procedure() RETURNS trigger AS $$ BEGIN INSERT INTO tbl_changelog(tbl, op, new, old) VALUES (TG_TABLE_NAME, TG_OP, row_to_json(NEW), row_to_json(OLD)); RETURN NULL; END; $$ LANGUAGE plpgsql

How to convert sql-text to jsonb-string?

戏子无情 提交于 2020-12-15 01:00:28
问题 There does not seem to be an obvious way: select 'a123'::text::jsonb = ERROR: invalid input syntax for type json select '"a123"'::text::jsonb = BAD string because quoted check select '"a123"'::text::jsonb = ('{"x":"a123"}'::jsonb)->'x' to see that non-quoted is the correct . select '123'::text::jsonb = ('{"x":123}'::jsonb)->'x'; = NOT string I need '123' and 'a123' as pure JSONb strings . PS: it is not a duplicate of generic automatic-anything conversion. 回答1: To convert untyped string

How to convert sql-text to jsonb-string?

こ雲淡風輕ζ 提交于 2020-12-15 00:52:01
问题 There does not seem to be an obvious way: select 'a123'::text::jsonb = ERROR: invalid input syntax for type json select '"a123"'::text::jsonb = BAD string because quoted check select '"a123"'::text::jsonb = ('{"x":"a123"}'::jsonb)->'x' to see that non-quoted is the correct . select '123'::text::jsonb = ('{"x":123}'::jsonb)->'x'; = NOT string I need '123' and 'a123' as pure JSONb strings . PS: it is not a duplicate of generic automatic-anything conversion. 回答1: To convert untyped string