One of my table has JSON data in a SQL Server database. I need to add new property to existing JSON.
Example of JSON data in Table:
{ "FirstName": "John" }
What I need is to add another property to this column.
{ "FirstName": "Jonh","City":"Pune" }
I tried to use JSON_MODIFY function but it is throwing error. Does this work only with array?
Code which I tried:
update <TableName>
set <jsonColumnName> = JSON_MODIFY(<jsonColumnName>, '$.', JSON_QUERY(N'{ "City":"Pune" }'))
where UserId = 1
Error message:
JSON path is not properly formatted. Unexpected character '.' is found at position 2.
4条答案
按热度按时间ffx8fchx1#
I think you want this syntax:
hl0ma9xz2#
Example to Add, Update and Delete a Property From JSON
Sample Table and Data
Insert New Property
Update Existing Property
Update Multiple Existing Properties
Delete Existing Property
ht4b089n3#
In your case, you need to use append with JSON_Modify. Something like this:
4xrmg8kj4#
I want to add that Gauravsa's answer is helpful when storing an object to the JSON property otherwise it would be stored as a deserialized object.
An example usage is: