I want to add a new array element to a3 at below json:
{
"a1": "e1",
"a2": {
"b1": "y1",
"b2": "y2"
},
"a3": [{
"arr1": "1"
},
{
"arr2": "2"
}
]
}
So I want above json to be like this one:
{
"a1": "e1",
"a2": {
"b1": "y1",
"b2": "y2"
},
"a3": [{
"arr1": "1"
},
{
"arr2": "2"
},
{
"arr3": "3"
}
]
}
I can add new element with below command. But when it comes to array I couldnt find a way to add new element.
SELECT jsonb_set('{ "a1": "e1", "a2": { "b1": "y1", "b2": "y2" }, "a3": [{ "arr1": "1" }, { "arr2": "2" }] }'::jsonb,
'{a2,b3}',
'"4"');
What command I sould use to add { "arr3": "3" } to a3?
edit: if arr3 already exists, the command should change its value. Shouldn't add duplicate {"arr3":"3"}.