I have JSON array value column in database:
[
{
"id": 1,
"name": "One"
},
{
"id": 2,
"name": "Two"
}
]
I want to add a new key-value into each object. Expected result is:
[
{
"id": 1,
"name": "One",
"active": "1"
},
{
"id": 2,
"name": "Two",
"active": "1"
}
]
By using JSON_MODIFY I can add a new key-value into one of the object as following:
UPDATE MyTable
SET JsonValueColumn=JSON_MODIFY(JsonValueColumn,'$[1].active','1')
WHERE Id = 'id'
But I need to add a new key-value into all objects in JSON array. I have an option to iterate in json objects, add new key-value, combine them again and update JsonValueColumn with the new value.
But I am looking if there is a simpler way to achieve this. Something like:
UPDATE MyTable
SET JsonValueColumn=JSON_MODIFY(JsonValueColumn,'$[*].active','1')
WHERE Id = 'id'