I have a task to remove a JSON property saved in a SQL Server database table's column. Here is the structure of my table
OrderId Name JSON
In the JSON
column I have this JSON data:
{
"Property1" :"",
"Property2" :"",
"metadata-department": "A",
"metadata-group": "B"
}
I have over 500 records that have this json value.
Can I update all the records to remove metadata-department
?
To remove JSON element, use the delete keyword in JavaScript.
You can use the UPDATE statement to modify values of a JSON column in the SET clause. You can only update the JSON column, not individual portions of the JSON instance. Also, when referencing a JSON column in its entirety, the input format is the same as the INSERT statement.
You can store JSON documents in SQL Server or SQL Database and query JSON data as in a NoSQL database.
The drawback? If your JSON has multiple fields with the same key, only one of them, the last one, will be retained. The other drawback is that MySQL doesn't support indexing JSON columns, which means that searching through your JSON documents could result in a full table scan.
This question is old but I thought I'd post another solution for those who may end up here via search engine.
In SQL Server 2016 or SQL Azure, the following works:
DECLARE @info NVARCHAR(100) = '{"name":"John","skills":["C#","SQL"]}'
PRINT JSON_MODIFY(@info, '$.name', NULL)
-- Result: {"skills":["C#","SQL"]}
Source: https://msdn.microsoft.com/en-us/library/dn921892.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With