sql-server-json

Concatenate or merge two json objects in SQL Server

风流意气都作罢 提交于 2020-01-01 10:04:21
问题 I have a table storing json in one column. I would like to update the json value by merging in another json. Something like: insert into mytable values ('{ "a": "b" ') update mytable set jsonColumn = JSON_MERGE(jsonColumn, '{ "c": 2 }') This should result in json like this: { "a": "b", "c": 2 } Unfortunately there is no such JSON_MERGE function and JSON_MODIFY lets me modify only columns one by one. I have too many of them including nested properties. I'm basically searching for an equivalent

SQL Server JSON_Modify, How to Update all?

这一生的挚爱 提交于 2019-12-21 13:41:12
问题 i am trying update all columns with a value with Json_Modify: DECLARE @JSON NVARCHAR(MAX) SET @JSON = N'{ "A":1, "TMP": [ {"A":"VALUE1", "B": "VALUE2", "C": 1}, {"A":"VALUE3", "B": "VALUE4", "C": 2}, {"A":"VALUE5", "B": "VALUE6", "C": 3}]} ' SET @JSON = JSON_MODIFY(@JSON, '$.TMP.A', 'JEJE') SELECT * FROM OPENJSON(@JSON, '$.TMP') WITH ( A NCHAR(10), B NCHAR(10), C INT ) I need update all columns "A" with "JEJE" for example, it is not working. 回答1: Here are two options. Disclaimer: I am not a

SQL Server FOR JSON Path Nested Array

情到浓时终转凉″ 提交于 2019-12-07 04:02:50
问题 We are trying to use FOR JSON Path in SQL Server 2016 for forming a Nested Array from a SQL Query. SQL Query: SELECT A, B.name as [child.name], B.date as [child.date] from Table 1 join Table 2 on Table 1.ID=Table 2.ID FOR JSON PATH Desired Output: [{ A:"text", "child:"[ {"name":"value", "date":"value"}, {"name":"value", "date":"value"} ] }] However what we are getting is: [{ A:"text", "child:" {"name":"value", "date":"value"} }, { A:"text", "child":{"name":"value", "date":"value"} }] How can

SQL Server FOR JSON Path Nested Array

荒凉一梦 提交于 2019-12-05 07:17:09
We are trying to use FOR JSON Path in SQL Server 2016 for forming a Nested Array from a SQL Query. SQL Query: SELECT A, B.name as [child.name], B.date as [child.date] from Table 1 join Table 2 on Table 1.ID=Table 2.ID FOR JSON PATH Desired Output: [{ A:"text", "child:"[ {"name":"value", "date":"value"}, {"name":"value", "date":"value"} ] }] However what we are getting is: [{ A:"text", "child:" {"name":"value", "date":"value"} }, { A:"text", "child":{"name":"value", "date":"value"} }] How can we use FOR JSON PATH to form nested child array. gordy instead of join use nested query, e.g.: SELECT A

SQL Server JSON_Modify, How to Update all?

老子叫甜甜 提交于 2019-12-04 07:04:15
i am trying update all columns with a value with Json_Modify: DECLARE @JSON NVARCHAR(MAX) SET @JSON = N'{ "A":1, "TMP": [ {"A":"VALUE1", "B": "VALUE2", "C": 1}, {"A":"VALUE3", "B": "VALUE4", "C": 2}, {"A":"VALUE5", "B": "VALUE6", "C": 3}]} ' SET @JSON = JSON_MODIFY(@JSON, '$.TMP.A', 'JEJE') SELECT * FROM OPENJSON(@JSON, '$.TMP') WITH ( A NCHAR(10), B NCHAR(10), C INT ) I need update all columns "A" with "JEJE" for example, it is not working. Here are two options. Disclaimer: I am not a pro at JSON through sql server 2016, but I have hacked some stuff together. Option 1: You are clearly