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.
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.
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.
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.
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.
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 )
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)
) + ']'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With