Sample Header Ad - 728x90

Update all the elements of JSON array nested in a JSON column (Postgres)

1 vote
2 answers
1810 views
I have json column with data that looks like this:
CREATE TABLE foo(mycol)
AS SELECT $${
    "a": [
        { "q": 5 },
        { "q": 8 },
        { "q": 10 }
    ]
}$$::jsonb;
I'd like to map over the elements in the array and add an extra property to get something like this:
{
    a: [
        { q: 5, r: [] },
        { q: 8, r: [] },
        { q: 10, r: [] }
    ]
}
Any and all would be greatly appreciated. PostgreSQL 9.6.6
Asked by mushishi78 (121 rep)
Aug 19, 2019, 12:57 PM
Last activity: May 27, 2025, 11:02 AM