9

I need to update a jsonb data(column->users) in my table 'settings' My jsonb data is like

'{
    "Email": "aaaa",
    "UserId": "49",
    "Created": "11/13/2016",
    "EntityId": "1",
    "IsActive": "False",
    "Modified": "11/13/2016",
    "Username": "aa"
}' 

In this json string I need to update Email,IsActive,Username together. I tried the below update query,its working fine. But that is for a single value updation.

UPDATE settings 
SET users = jsonb_set(users, '{Email}', '"aa"') 
WHERE users @> '{"UserId":"49"}';

How to update for multiple value updation? I am using postgres 9.5.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ajoe
  • 1,397
  • 4
  • 19
  • 48
  • Possible duplicate of [postgres jsonb\_set multiple keys update](https://stackoverflow.com/questions/38883233/postgres-jsonb-set-multiple-keys-update) – uLan Oct 26 '17 at 03:51

1 Answers1

26

Use the concatenation operator:

UPDATE settings 
SET users = users || '{"Email": "new email", "IsActive": "True", "Username": "new username"}'
WHERE users @> '{"UserId":"49"}';
klin
  • 112,967
  • 15
  • 204
  • 232