0

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))**
0
1

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)
               )
;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.