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.
Postgres offers a jsonb_set function for updating JSON fields. The second parameter path defines, which property you want to update. To update items in an array, you can use an index-based approach. To update the first entry in the items array in the example above, a path woud look like this: {items, 0, customerId} .
Querying the JSON documentPostgreSQL has two native operators -> and ->> to query JSON documents. The first operator -> returns a JSON object, while the operator ->> returns text. These operators work on both JSON as well as JSONB columns. There are additional operators available for JSONB columns.
Because JSONB stores data in a binary format, queries process significantly faster. Storing data in binary form allows Postgres to access a particular JSON key-value pair without reading the entire JSON record. The reduced disk load speeds up overall performance.
Use the concatenation operator:
UPDATE settings
SET users = users || '{"Email": "new email", "IsActive": "True", "Username": "new username"}'
WHERE users @> '{"UserId":"49"}';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With