0

I have a SQL table Templates, which has a column JsonContent. I would like to modify the content of JsonContent with SQL.

Each JsonContent has serialized JSON array of objects. For example:

[
   {
      "Name":"Test name",
      "Options":[
         {
            "Name":"1",
            "Value":1
         },
         {
            "Name":"2",
            "Value":2
         },
         {
            "Name":"3",
            "Value":3
         }
      ]
   },
   {
      "Name":"Another name",
      "Options":null
   }
]

I would like to add new property to each object in Options, which will be named IsDeleted and set the value to false. For example:

[
   {
      "Name":"Test name",
      "Options":[
         {
            "Name":"1",
            "Value":1,
            "IsDeleted": false
         },
         {
            "Name":"2",
            "Value":2,
            "IsDeleted": false
         },
         {
            "Name":"3",
            "Value":3,
            "IsDeleted": false
         }
      ]
   },
   {
      "Name":"Another name",
      "Options":null
   }
]

How can I modify the first example with SQL and get the second example as a result?

3
  • see stackoverflow.com/questions/59292086/… Commented Jan 14 at 13:21
  • Does the stored JSON always have this fixed structure? And what is the SQL Server version? Commented Jan 15 at 6:45
  • @Zhorov, yes the JSON has fixed structure. I'm using v19 Commented Jan 15 at 7:57

2 Answers 2

3

The JSON_MODIFY(expression, path, newValue) function is usually used to update the value of an existing JSON property or insert (delete) a specified "key":value pair. The problem here is that this function doesn't support wildcard characters for the path parameter.

One possible approach in this situation (but only if the stored JSON has a fixed structure) are the following steps:

  • Transform the JSON content into tables using two nested OPENJSON() calls with the appropriate explicit schemas.
  • Include the new JSON property as column.
  • Output the tables as JSON using FOR JSON.

T-SQL:

UPDATE Templates
SET JsonContent = (  
   SELECT 
      Name,
      Options = (
         SELECT [Name], [Value], CAST(0 as bit) AS [IsDeleted]
         FROM OPENJSON(Options) WITH (
            [Name] nvarchar(max) '$.Name',
            [Value] int '$.Value' 
         )
         FOR JSON PATH, INCLUDE_NULL_VALUES
      )
   FROM OPENJSON(@json) WITH (
      Name nvarchar(max) '$.Name',
      Options nvarchar(max) '$.Options' AS JSON
   )
   FOR JSON PATH, INCLUDE_NULL_VALUES
)

As a note, on Azure SQL Database (and probably on SQL Server 2025), you may use the JSON_ARRAYAGG() function to construct a JSON array from an aggregation of SQL data or columns.

Sign up to request clarification or add additional context in comments.

Comments

0

Using the example from How to update insert JSON property in an array of object with T-SQL I applied your scenario:

SELECT @json = JSON_MODIFY(@json, CONCAT('$.Options[', [key], '].IsDeleted'), 'false')
FROM OPENJSON(@json, '$.Options')

1 Comment

This works for single object, not for array of objects. I need to iterate each object in the array and do execute the query you sent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.