I am stuck with the json arrays into SQL Server. I followed this tutorial https://learn.microsoft.com/en-us/sql/relational-databases/json/json-data-sql-server?view=sql-server-ver15, but I cannot figure out how to push to the existing array.
It works fine with an array of strings:
SET @info=JSON_MODIFY(@info, 'append $.skills', 'Azure')
but it seems it doesn't work with objects (or I am missing something here).
This is what I've tried so far :
declare @json nvarchar(max) = '{
"changes":[
{
"logid":1,
"changedfield":"site",
"oldvalue":"0",
"newvalue":"637049"
},
{
"logid":2,
"changedfield":"site",
"oldvalue":"637049",
"newvalue":"637041"
}
]
}'
select ISJSON(@json) -- 1
select JSON_QUERY(@json, '$.changes')
--displays
--[
-- {
-- "logid":1,
-- "changedfield":"site",
-- "oldvalue":"0",
-- "newvalue":"637049"
-- },
-- {
-- "contactlogid":516870,
-- "changedfield":"site",
-- "oldvalue":"0",
-- "newvalue":"637049"
-- }
--]
-- now I want to push to the changes array
declare @json1 as nvarchar(max) = '{
"changes":[
{
"logid":1,
"changedfield":"site",
"oldvalue":"0",
"newvalue":"637049"
},
{
"logid":2,
"changedfield":"site",
"oldvalue":"637049",
"newvalue":"637041"
}
]
}'
select JSON_MODIFY(@json, 'append $.changes', @json1)
-- the result
--{
-- "changes":[
-- {
-- "logid":1,
-- "changedfield":"site",
-- "oldvalue":"0",
-- "newvalue":"637049"
-- },
-- {
-- "logid":2,
-- "changedfield":"site",
-- "oldvalue":"637049",
-- "newvalue":"637041"
-- },
-- "{\r\n \"changes\":[\r\n {\r\n \"logid\":1,\r\n \"changedfield\":\"site\",\r\n \"oldvalue\":\"0\",\r\n \"newvalue\":\"637049\"\r\n },\r\n {\r\n \"logid\":2,\r\n \"changedfield\":\"site\",\r\n \"oldvalue\":\"637049\",\r\n \"newvalue\":\"637041\"\r\n }\r\n\t ]\r\n}"
-- ]
--}
select JSON_MODIFY(@json, 'append $.changes', (select json_query(@json1,'$.changes')))
-- result -- it creates two arrays
--{
-- "changes":[
-- {
-- "logid":1,
-- "changedfield":"site",
-- "oldvalue":"0",
-- "newvalue":"637049"
-- },
-- {
-- "logid":2,
-- "changedfield":"site",
-- "oldvalue":"637049",
-- "newvalue":"637041"
-- },
-- [
-- {
-- "logid":1,
-- "changedfield":"site",
-- "oldvalue":"0",
-- "newvalue":"637049"
-- },
-- {
-- "logid":2,
-- "changedfield":"site",
-- "oldvalue":"637049",
-- "newvalue":"637041"
-- }
-- ]
-- ]
--}
Do you have any idea how to push an object into the existing array?