I have a table, let's call it myTable with the following structure
ID | data
____________
uuid | jsonb
The data in the jsonb field is an array structured in the following way:
[
{
"valueA": "500",
"valueB": "ABC",
},
{
"valueA": "300",
"valueB": "CDE",
}
]
What I want to do is transform that data by adding a valueC and set it to valueA, for all the objects inside the 'data' jsonb array.
This is the result I want:
[
{
"valueA": "500",
"valueB": "ABC",
"valueC": "500",
},
{
"valueA": "300",
"valueB": "CDE",
"valueC": "300",
}
]
I tried doing it with the following query:
UPDATE myTable
SET data = d.json_array
FROM (
SELECT
jsonb_agg(
jsonb_set(elems, '{valueC}', elems->'valueA')
) as json_array
FROM
myTable,
jsonb_array_elements(data) elems
) d;
This worked for some on the entries in myTable, but for some it went crazy and created 300+ additional entries, along with the ones I previosly had.
What am I missing in my query?