0

I have json string in sql db as this

{
   "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"
            }
         }
      ]
   }
}

you can parse on https://jsonparseronline.com/ to view json

I need to update $.MainItem.ListSubItems where UserId=3 set UserName ='new name'

I need update all userid = 3

as

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

1 Answer 1

2

If i'm following what you are trying to do, this should work. I don't know how many records are in your table so some tweaking/tuning may be necessary.

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
Sign up to request clarification or add additional context in comments.

Comments

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.