How to update more than 1 properties of jsonb field

postgresql-13

how to update the age and the status at once,

I have a jsonb field type, I need to update 2 properties (age and status).
I am able to update only either age and status, using the command below, how to do update both 'age' and 'status' at once?

**Update test Set attributes = jsonb_set(attributes, array['age'],to_jsonb(32))**

Best Answer

The jsonb_set function accepts a jsonb value as its first argument and returns another jsonb value, which is the result of the modification applied. You can pass the resulting jsonb value to another jsonb_set call where you would apply the second change. This would result in nested jsonb_set calls, as a_horse_with_no_name suggested in the comments. The statement would look something like this:

UPDATE
  test
SET
  attributes = jsonb_set(
                 jsonb_set(attributes, array['age'], to_jsonb(32))
               , array['status']
               , to_jsonb('some new status'::text)
               )
;
Related Question