4

I'm trying to run the following but it's not updating columns with NULL values or columns that do not have this key:

update A a set 
a.jsonbcolumn = ('{"key":' 1 '}')

I'm using postgres 9.6.3 and jsonb_set does not work for me. Any advice?

Thanks

1 Answer 1

5

jsonb_set() is the way to go:

update a
  set jsonbcolumn = jsonb_set(jsonbcolumn, '{key}', '1');

If it does not create the key, this is probably becase the column value is NULL rather than an empty JSON. In that case use coalesce():

update a
  set jsonbcolumn = jsonb_set(coalesce(jsonbcolumn,'{}'), '{key}', '1');

Online example: https://rextester.com/MSU66547

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.