1

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))**
Don2
  • 429
  • 1
  • 6
  • 8

1 Answers1

4

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)
               )
;
Andriy M
  • 22,983
  • 6
  • 59
  • 103