Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server JSON_Modify, How to Update all?

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.

like image 394
mdelphi Avatar asked Dec 29 '16 16:12

mdelphi


People also ask

How do I update a JSON column in SQL?

You can use the UPDATE statement to modify values of a JSON column in the SET clause. You can only update the JSON column, not individual portions of the JSON instance. Also, when referencing a JSON column in its entirety, the input format is the same as the INSERT statement. MERGE.

What is Json_modify in SQL Server?

The new value for the property specified by path. The new value must be a [n]varchar or text. In lax mode, JSON_MODIFY deletes the specified key if the new value is NULL. JSON_MODIFY escapes all special characters in the new value if the type of the value is NVARCHAR or VARCHAR.

How do I change the value of a JSON file?

First you would need to convert it to a JavaScript Object. Once it is an Object, then you can just use dot notation into the object to change the values that you want. Lastly, you would convert that JavaScript Object back into a JSON string.

Which method will you use if you need to update a particular data JSON?

We use JSON_MODIFY() function to update the JSON string. It can update the following items: Update existing property value. Add a new element in an existing array.


2 Answers

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 creating a result set from the JSON string. Why not create the result set and then update it?

DECLARE @jsontable TABLE (A varchar(50), b varchar(50), c varchar(50))
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')
INSERT INTO @jsontable (a,b,c)
SELECT * FROM OPENJSON(@JSON, '$.TMP') WITH ( A NCHAR(10), B NCHAR(10), C INT )

UPDATE @jsontable 
SET a = 'JEJE' 

SELECT * 
FROM @jsontable

Option 2: you can manipulate the JSON components, but you have to pass the index to the function.

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[0].A', 'JEJE')
SET @JSON = JSON_MODIFY(@JSON, '$.TMP[1].A', 'JEJE')
SET @JSON = JSON_MODIFY(@JSON, '$.TMP[2].A', 'JEJE')

SELECT * FROM OPENJSON(@JSON, '$.TMP') WITH ( A NCHAR(10), B NCHAR(10), C INT )
like image 146
dfundako Avatar answered Oct 13 '22 00:10

dfundako


If you are storing the array in a field, you can do the following:

UPDATE some_table SET
    some_field = '[' + (
        SELECT
            STRING_AGG(JSON_MODIFY([value],'$.A', 'JEJE'), ',') WITHIN GROUP (ORDER BY CAST([key] AS int))
        FROM
            OPENJSON(some_field)
    ) + ']'
like image 31
drowa Avatar answered Oct 13 '22 00:10

drowa