Created
March 12, 2020 14:32
-
-
Save dimitrilahaye/e26b7027dcc84bc8e1593d72c8926512 to your computer and use it in GitHub Desktop.
PostgresSQL => Some CRUD operations with JSONB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-------------------------------------------------------- | |
-- Add key into a nested object and hard code its value | |
-------------------------------------------------------- | |
UPDATE my.table | |
SET data = jsonb_set(data::jsonb, '{"user","config","application"}', '"GOOGLE"'); | |
/* | |
-- before | |
{ | |
"user": { | |
"fullname": "Barney Stinson", | |
"job": "unknown", | |
"config": { | |
"notifications": false, | |
"theme": "dark" | |
} | |
} | |
} | |
-- after | |
{ | |
"user": { | |
"fullname": "Barney Stinson", | |
"job": "unknown", | |
"config": { | |
"notifications": false, | |
"theme": "dark", | |
"application": "GOOGLE" | |
} | |
} | |
} | |
*/ | |
-------------------------------------------------------- | |
-- Copy key:value from a nested object to another | |
-------------------------------------------------------- | |
UPDATE my.table | |
SET data = jsonb_set(data::jsonb, '{"user","config","job"}', data::jsonb#>'{user,job}'); | |
/* | |
-- before | |
{ | |
"user": { | |
"fullname": "Barney Stinson", | |
"job": "unknown", | |
"config": { | |
"notifications": false, | |
"theme": "dark" | |
} | |
} | |
} | |
-- after | |
{ | |
"user": { | |
"fullname": "Barney Stinson", | |
"job": "unknown", | |
"config": { | |
"notifications": false, | |
"theme": "dark", | |
"job": "unknown" | |
} | |
} | |
} | |
*/ | |
-------------------------------------------------------- | |
-- Add key into a nested object and get its value from another table column | |
-------------------------------------------------------- | |
UPDATE my.table | |
SET data = jsonb_set(data::jsonb, '{"user","config","job"}', to_json(job)::jsonb); | |
/* | |
-- before | |
{ | |
"user": { | |
"fullname": "Barney Stinson", | |
"job": "unknown", | |
"config": { | |
"notifications": false, | |
"theme": "dark" | |
} | |
} | |
} | |
-- after | |
{ | |
"user": { | |
"fullname": "Barney Stinson", | |
"job": "unknown", | |
"config": { | |
"notifications": false, | |
"theme": "dark", | |
"job": "Job result from another column" | |
} | |
} | |
} | |
*/ | |
-------------------------------------------------------- | |
-- remove nested object key:value | |
-------------------------------------------------------- | |
UPDATE my.table | |
SET data = data::jsonb #- '{user,job}'; | |
/* | |
-- before | |
{ | |
"user": { | |
"fullname": "Barney Stinson", | |
"job": "unknown", | |
"config": { | |
"notifications": false, | |
"theme": "dark" | |
} | |
} | |
} | |
-- after | |
{ | |
"user": { | |
"fullname": "Barney Stinson", | |
"config": { | |
"notifications": false, | |
"theme": "dark" | |
} | |
} | |
} | |
*/ | |
-------------------------------------------------------- | |
-- rename nested object key | |
-------------------------------------------------------- | |
UPDATE my.table | |
SET data = jsonb_set(data::jsonb #- '{user,job}', '{user,profession}', data::jsonb#>'{user,job}'); | |
/* | |
-- before | |
{ | |
"user": { | |
"fullname": "Barney Stinson", | |
"job": "unknown", | |
"config": { | |
"notifications": false, | |
"theme": "dark" | |
} | |
} | |
} | |
-- after | |
{ | |
"user": { | |
"fullname": "Barney Stinson", | |
"profession": "unknown", | |
"config": { | |
"notifications": false, | |
"theme": "dark" | |
} | |
} | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment