Append json value

  • Hi all,
    I have json like this
    DECLARE @j-2 NVARCHAR(max) = N'[{"Views":1,"Date":1493119613,"MediaCaption":"test"}]'

    i want to add  "size"="1000" to this array

    SET @j-2 = JSON_MODIFY(@j,'append $[0].size' , 1000)
    SELECT @j-2

    but It append "size":[1000]
    how can Id add  "size"="1000" instead of "size":[1000] ?

  • farax_x - Saturday, April 29, 2017 4:39 AM

    Hi all,
    I have json like this
    DECLARE @j-2 NVARCHAR(max) = N'[{"Views":1,"Date":1493119613,"MediaCaption":"test"}]'

    i want to add  "size"="1000" to this array

    SET @j-2 = JSON_MODIFY(@j,'append $[0].size' , 1000)
    SELECT @j-2

    but It append "size":[1000]
    how can Id add  "size"="1000" instead of "size":[1000] ?

    Interesting, not by a 2016 box right now but have you tried skipping the enumeration?
    😎
    SET @j-2 = JSON_MODIFY(@j,'append $.size' , 1000)
    SELECT @j-2

  • Eirikur Eiriksson - Saturday, April 29, 2017 7:06 AM

    farax_x - Saturday, April 29, 2017 4:39 AM

    Hi all,
    I have json like this
    DECLARE @j-2 NVARCHAR(max) = N'[{"Views":1,"Date":1493119613,"MediaCaption":"test"}]'

    i want to add  "size"="1000" to this array

    SET @j-2 = JSON_MODIFY(@j,'append $[0].size' , 1000)
    SELECT @j-2

    but It append "size":[1000]
    how can Id add  "size"="1000" instead of "size":[1000] ?

    Interesting, not by a 2016 box right now but have you tried skipping the enumeration?
    😎
    SET @j-2 = JSON_MODIFY(@j,'append $.size' , 1000)
    SELECT @j-2

    Yes, but I doesn't work !

  • Hi,

    Have you tried enclosing  the value in single-quotes?

    I mean JSON_MODIFY(@j, 'append $.size', '1000')

    Please let us know.

  • debasis.yours - Saturday, April 29, 2017 10:27 AM

    Hi,

    Have you tried enclosing  the value in single-quotes?

    I mean JSON_MODIFY(@j, 'append $.size', '1000')

    Please let us know.

    My problem is about putting array instead of single value 

    "size"="1000" ==>ok
    "size":["1000"] ==>not ok

  • Maybe without append:

    DECLARE @j-2 NVARCHAR(max) = N'[{"Views":1,"Date":1493119613,"MediaCaption":"test"}]'

    SET @j-2 = JSON_MODIFY(@j-2,'$[0].size' , 1000)

    SELECT @j-2

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply