最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Append JSON values to a JSON array in SQL Server? - Stack Overflow

programmeradmin1浏览0评论

I am trying to append some JSON values to a JSON array in SQL Server.

Here is what I have tried from other similar questions asked.

DECLARE @features NVARCHAR(500)='{"Name": "Feature1","IsEnabled": false},{"Name": "Feature2","IsEnabled": true},{"Name": "Feature3","IsEnabled": false}'

UPDATE JsonTable
   SET 
    Content = JSON_MODIFY (
      Content, 
      'append $.Features', 
      JSON_QUERY(@features)
      )

But it is only appending "Name": "Feature1","IsEnabled": false and not the rest.

My current Content looks something like this

{
  "Features": [
    {
      "Name": "Feature0",
      "IsEnabled": true
    }
  ],
  "SpecialFeatures": [
    {
      "Name": "SFeature0",
      "IsEnabled": false
    }
  ]
}

and the expected results is like below

{
  "Features": [
    {
      "Name": "Feature0",
      "IsEnabled": true
    },
    {
      "Name": "Feature1",
      "IsEnabled": false
    },
    {
      "Name": "Feature2",
      "IsEnabled": true
    },
    {
      "Name": "Feature3",
      "IsEnabled": false
    }
  ],
  "SpecialFeatures": [
    {
      "Name": "SFeature0",
      "IsEnabled": false
    }
  ]
}

Can anyone help on the same?

I am trying to append some JSON values to a JSON array in SQL Server.

Here is what I have tried from other similar questions asked.

DECLARE @features NVARCHAR(500)='{"Name": "Feature1","IsEnabled": false},{"Name": "Feature2","IsEnabled": true},{"Name": "Feature3","IsEnabled": false}'

UPDATE JsonTable
   SET 
    Content = JSON_MODIFY (
      Content, 
      'append $.Features', 
      JSON_QUERY(@features)
      )

But it is only appending "Name": "Feature1","IsEnabled": false and not the rest.

My current Content looks something like this

{
  "Features": [
    {
      "Name": "Feature0",
      "IsEnabled": true
    }
  ],
  "SpecialFeatures": [
    {
      "Name": "SFeature0",
      "IsEnabled": false
    }
  ]
}

and the expected results is like below

{
  "Features": [
    {
      "Name": "Feature0",
      "IsEnabled": true
    },
    {
      "Name": "Feature1",
      "IsEnabled": false
    },
    {
      "Name": "Feature2",
      "IsEnabled": true
    },
    {
      "Name": "Feature3",
      "IsEnabled": false
    }
  ],
  "SpecialFeatures": [
    {
      "Name": "SFeature0",
      "IsEnabled": false
    }
  ]
}

Can anyone help on the same?

Share Improve this question edited Feb 6 at 18:31 Dale K 27.3k15 gold badges56 silver badges82 bronze badges asked Feb 6 at 10:48 Laba NingombamLaba Ningombam 1631 gold badge3 silver badges10 bronze badges 4
  • 1 If you run select JSON_QUERY(@features) it should make things clear. You aren't appending proper json – siggemannen Commented Feb 6 at 10:51
  • @siggemannen, I see it now. If I add [ ], it gives a proper json but it's not appending in the correct "Features" that I want it to, as it is creating a separate json array. – Laba Ningombam Commented Feb 6 at 11:04
  • Can you provide us with sample data and expected results? – Thom A Commented Feb 6 at 11:05
  • @ThomA, updated the question with more details – Laba Ningombam Commented Feb 6 at 11:07
Add a comment  | 

1 Answer 1

Reset to default 2

JSON_MODIFY will not append multiple values to an array even if you use append , it's only designed for single values or objects. Also your JSON is not valid as it's missing the [] array delimiters.

You can instead rebuild your JSON in a subquery.

DECLARE @features NVARCHAR(500)='[{"Name": "Feature1","IsEnabled": false},{"Name": "Feature2","IsEnabled": true},{"Name": "Feature3","IsEnabled": false}]'

UPDATE JsonTable
   SET 
    Content = JSON_MODIFY (
      Content, 
      '$.Features', 
        (
          SELECT j.*
          FROM (
              SELECT j1.*
              FROM OPENJSON(Content, '$.Features')
                WITH (
                  Name nvarchar(100),
                  IsEnabled bit
                ) j1
              UNION ALL
              SELECT j2.*
              FROM OPENJSON(@features)
                WITH (
                  Name nvarchar(100),
                  IsEnabled bit
                ) j2
          ) j
          FOR JSON PATH
        )
      )

SQL Fiddle

In Azure SQL and SQL Server 2025 you can use JSON_ARRAYAGG

DECLARE @features NVARCHAR(500)='[{"Name": "Feature1","IsEnabled": false},{"Name": "Feature2","IsEnabled": true},{"Name": "Feature3","IsEnabled": false}]'

UPDATE JsonTable
   SET 
    Content = JSON_MODIFY (
      Content, 
      '$.Features', 
        (
          SELECT JSON_ARRAYAGG(JSON_QUERY(j.value) ORDER BY Ordering, CAST([key] AS int))
          FROM (
              SELECT *, 1 AS Ordering
              FROM OPENJSON(Content, '$.Features')
              UNION ALL
              SELECT *, 2
              FROM OPENJSON(@features)
          ) j
        )
      )
发布评论

评论列表(0)

  1. 暂无评论