I have a basic Postgres table structure:
CREATE TABLE PROPS(id integer, data jsonb);
and format of data column:
[
{
"name": "abc",
"refKey": 123
},
{
"name": "cba",
"refKey": 124
},
{
"name": "xyz",
"refKey": 123
}
]
I am struggling to write a sql to update "refKey" from 123 to 125 for all data (entire PROPS table).
Here is a query that can do that:
WITH src AS (
SELECT id, json_array_elements(data::JSON) as arr FROM Props
), modified AS (
SELECT id, arr::JSONB || '{"refKey":125}'::JSONB AS arr FROM src
), regrouped AS (
SELECT id, json_agg(arr) AS data FROM modified GROUP BY id
)
UPDATE Props
SET "data" = regrouped."data"
FROM regrouped
WHERE regrouped.id = Props."id";
It's definitely a little cumbersome but I found it easier to show the steps with the intermediate CTEs (Common Table Expressions).
Here's what we do in order:
json_array_elements()
function.||
)json_agg
function.Obviously you can customize the WHERE
segment of the UPDATE
command to only update certain rows, and you can update the logic in the modified
CTE to update differently (e.g. increment instead of just setting to 125).