I have table with Jsonb field on it and need to update data of jsonb fields.
Data stored like below:
[{"DOB": "04-May-2006", "Code": "2009585", "Class" : "I - Noon"}]
I need to Replace Class Key from ‘Noon’ to ‘NS’.
I have also check out existing answer but that all are work for data with single object like below
{"DOB": "04-May-2006", "Code": "2009585", "Class" : "I - Noon"}
I need to update in with multiple object []
here is fiddle for the same to try
3
Answers
Use jsonb_set
See: https://dbfiddle.uk/JJu4oxx8
You may need this:
Use a subquery to extract all elements from the array, change the content and create a new fresh array:
You can use
jsonb_set
andREPLACE
to update your object :If you have more than one element in your array then use
jsonb_array_elements
to generate row for each object, andjsonb_agg
to groupe those updated objects :Demo here