How to add property to JSON object in SQL Server column

I think you want this syntax:

UPDATE <TableName>
SET <jsonColumnName>=JSON_MODIFY(<jsonColumnName>,'$.City','Pune')
WHERE UserId=1

Example to Add, Update and Delete a Property From JSON

Sample Table and Data

declare @Table table
    (JsonColumn varchar(max))

insert into @Table
values
('{"FirstName": "John"}')

select * from  @Table

Insert New Property

UPDATE @Table
SET JsonColumn=JSON_MODIFY(JsonColumn,'$.City','Pune')

select * from  @Table

Update Existing Property

UPDATE @Table
SET JsonColumn=JSON_MODIFY(JsonColumn,'$.City','Delhi')

select * from  @Table

Update Multiple Existing Properties

UPDATE @Table
SET JsonColumn= JSON_MODIFY(JSON_MODIFY(JsonColumn,'$.City','Delhi'), '$.FirstName', 'Mukesh')

select * from  @Table

Delete Existing Property

UPDATE @Table
SET JsonColumn=JSON_MODIFY(JsonColumn,'$.City',null)

select * from  @Table

Tags:

Sql Server