如何修改json subArray sql server中的特定对象

Posted

技术标签:

【中文标题】如何修改json subArray sql server中的特定对象【英文标题】:How Modify specific object in json subArray sql server 【发布时间】:2020-06-17 16:00:46 【问题描述】:

我在sql db中有json字符串


   "MainItem":
      "UserId":4,
      "UserName":"name",
      "CityDetails":
         "CityId":1,
         "CityName":"egypt"
      ,
      "ListSubItems":[
         
            "UserId":2,
            "UserName":"name2",
            "FullDescription":"",
            "ShortDescription":"wish",
            "CityDetails":
               "CityId":2,
               "CityName":"us"
            
         ,
         
            "UserId":3,
            "UserName":"name3",
            "FullDescription":"",
            "ShortDescription":"wish",
            "CityDetails":
               "CityId":44,
               "CityName":"sau"
            
         
      ]
   

你可以解析https://jsonparseronline.com/来查看json

我需要更新$.MainItem.ListSubItems where UserId=3 set UserName ='new name'

我需要更新所有用户 ID = 3

作为

update MyTable 
  set jsonCol= json_modify(jsonCol, $.MainItem.ListSubItems .where userId=3 , userName='new name ')

【问题讨论】:

【参考方案1】:

如果我按照您正在尝试做的事情,这应该工作。我不知道您的表中有多少记录,因此可能需要进行一些调整/调整。

DECLARE @MyTable TABLE
(
    ID INT IDENTITY (1,1) NOT NULL
    ,JsonCol NVARCHAR(MAX)
)

INSERT INTO @MyTable
(
    JsonCol
)
VALUES
(N'
   "MainItem":
      "UserId":4,
      "UserName":"name",
      "CityDetails":
         "CityId":1,
         "CityName":"egypt"
      ,
      "ListSubItems":[
         
            "UserId":2,
            "UserName":"name2",
            "FullDescription":"",
            "ShortDescription":"wish",
            "CityDetails":
               "CityId":2,
               "CityName":"us"
            
         ,
         
            "UserId":3,
            "UserName":"name3",
            "FullDescription":"",
            "ShortDescription":"wish",
            "CityDetails":
               "CityId":44,
               "CityName":"sau"
            
         
      ]
   
'
)


UPDATE @MyTable
Set JsonCol = 
    JSON_MODIFY
        (jsonCol
        ,'$.MainItem.ListSubItems'
        ,(select 
        orig.UserId
        ,COALESCE(NewVals.UserName, orig.UserName) as UserName
        ,orig.FullDescription
        ,orig.ShortDescription
        ,orig.CityDetails
    from OPENJSON(jsonCol, '$.MainItem.ListSubItems') 
    WITH (
        UserId INT 
        ,UserName NVARCHAR(100)
        ,FullDescription NVARCHAR(1000)
        ,ShortDescription NVARCHAR(250)
        ,CityDetails NVARCHAR(MAX) as json
    ) as orig
    left join 
        (
            SELECT 3 as UserID, 'new name' as UserName
        ) as NewVals ON orig.UserId = NewVals.UserID
    FOR JSON AUTO)
    )

SELECT * FROM @MyTable

【讨论】:

以上是关于如何修改json subArray sql server中的特定对象的主要内容,如果未能解决你的问题,请参考以下文章

json Esqueleto做JSON que deve ser Gerado

如何修改postgres数据库中的json字段

json Exemplo de JSON que deve ser gerado pelo programa

如何在反应中从 json 构建 SQL 查询?

python 为啥我修改列表一个元素会导致所有元素都被修改

如何修改从使用 Json.Net 序列化数据集获得的 JSON 以用于 ESRI 地理编码