I have simple table table_b
id (integer) | data (json) | text (text) |
---|---|---|
1 | {} | yes |
2 | {} | no |
Json look like
{"types": [{"key": "first_event", "value": false}, {"key": "second_event", "value": false}, {"key": "third_event", "value": false}...]}
I just want to modify data and add to each json object in array ["test1", "test2"] to look like this :
{"types": [{"key": "first_event", "value": false, "can":["test1", "test2"] }, {"key": "second_event", "value": false , "can":["test1", "test2"]}, {"key": "third_event", "value": false , "can":["test1", "test2"]}...]}
ofcourse only where text like "yes"
I have tryed :
UPDATE table_b
SET data = jsonb_set(data , '{types,can}', '["test1", "test2"]'::jsonb, true)
where text like 'yes';
But it does not work. How can i loop over table_b and over data column?
2
Answers
The array value of a JSON object can be modified. It can be simply done by modifying the value present at a given index.
Using this sample data
This query updates each array element, keeping the order in the
ORDINALITY
columnThe next query concatenates the updated elements back to the array (keeping the right order) and performs a simple update