postgresql: jsonb update multiple keys in one query

ApathyBear picture ApathyBear · Oct 31, 2016 · Viewed 7.1k times · Source

I have the following postgresql rows as JSONB row:

{age:26}

And I would like to replace it so that that i looks like this:

{age: 30, city: "new york city"}

How can I do this in postgressql? Someone metioned using jsonb_set(), but I haven't seen any examples of updating multiple keys in one query.

Answer

SHAKU picture SHAKU · Dec 18, 2016

Use jsonb_set() inside jsonb_set()

jsonb_set(jsonb_set('{age:26}'::jsonb,'{age}','"30"'::jsonb)::jsonb,'{city}','"new york city"'::jsonb)