T-SQL Json_modify to append a property to each object

Viewed 112

I have a stored procedure that accepts a JSON string as input parameter. The input JSON string is like this:

[
    {
        "name":"Jhon",
        "surname":"Smith",
        "skills":["C#","VB.NET"]
    },
    {
        "name":"Robert",
        "surname":"Jhonson",
        "skills":["T-SQL","Pascal"]
    }
]

How can I add a unique GUID property to each principal object automatically?

1 Answers

Looking at your example data you already discovered this page of the documentation that tells you how to insert values with the json_modify() function. The examples on that page are written for a single "principal object".

If I interpret this correctly, then your sample has 2 principle objects. Using another page of the documentation shows how you can split that sample in rows with the openjson() function. You can then apply the json_modify() from the first documentation page on each row.

declare @var nvarchar(max) =
'[
   {
     "name":"Jhon",
     "surname":"Smith",
     "skills":["C#","VB.NET"]
   },
   {
     "name":"Robert",
     "surname":"Jhonson",
     "skills":["T-SQL","Pascal"]
   }
]';

select row_number() over(order by (select null)) as ObjNumber,
       json_modify(j.value, '$.guid', convert(nvarchar(100), newid())) as ObjValue
from openjson(@var, '$') j

The result looks like this:

ObjNumber   ObjValue
----------- ----------------------------------------------------
1           {
                "name":"Jhon",
                "surname":"Smith",
                "skills":["C#","VB.NET"]
                ,"guid":"154C5581-588C-41AA-B292-BB6459F8F4DC"}
2           {
                "name":"Robert",
                "surname":"Jhonson",
                "skills":["T-SQL","Pascal"]
                ,"guid":"46ACFDD6-58DE-4DB0-8D7A-9B1CCABFF8D8"}

Fiddle

To add the rows back together, just add for json path. This does however require a field alias (here MyObjects) that ends up in the output.

select json_modify(j.value, '$.guid', convert(nvarchar(100), newid())) as MyObjects
from openjson(@var, '$') j
for json path;

Output:

[{"MyObjects":{
     "name":"Jhon",
     "surname":"Smith",
     "skills":["C#","VB.NET"]
   ,"guid":"FCED4D30-B2B0-460B-97FA-EDA820039572"}},{"MyObjects":{
     "name":"Robert",
     "surname":"Jhonson",
     "skills":["T-SQL","Pascal"]
   ,"guid":"9FF02A70-0455-4E5C-8C11-27BB2688929D"}}]

Fiddle

To update the variable use the following code. Bonus: replace() removes the previously added field alias.

set @var = replace(
             ( select json_modify(j.value, '$.guid', convert(nvarchar(100), newid())) as MyObjects
               from openjson(@var, '$') j
               for json path ),
             '"MyObjects":', '');

Final output for select @var:

[{{
     "name":"Jhon",
     "surname":"Smith",
     "skills":["C#","VB.NET"]
   ,"guid":"66CB37D3-FAEF-4186-94D8-8AC0CF6EB1AC"}},{{
     "name":"Robert",
     "surname":"Jhonson",
     "skills":["T-SQL","Pascal"]
   ,"guid":"564D6904-D981-40AC-BA9C-8B06015ACE50"}}]

Fiddle

Related