Firstly I stored my JSON in a varchar column, now I got a requirement to change on of the fields in the JSON. How to retrieve the field and update it?
Example of one record
{ "name": "Bob", isSelected: "true" }
True needs to be changed to false
Firstly I stored my JSON in a varchar column, now I got a requirement to change on of the fields in the JSON. How to retrieve the field and update it?
Example of one record
{ "name": "Bob", isSelected: "true" }
True needs to be changed to false
If your JSON is properly formatted (it's not in your question as isSelected isn't quoted), then you use can JSON_MODIFY:
DECLARE @json nvarchar(MAX) = N'{"name": "Bob", "isSelected":"true" }';
SELECT JSON_MODIFY(@json, '$.isSelected', 'false');
If it isn't properly formatted, fix your process that creates the JSON first, to create well formed JSON, and then use JSON_MODIFY.
JSON_MODIFY is available in all fully supported versions of SQL Server and 2016. If you are getting that error, that likely means you're using a much older version of SQL Server; this is something you need to tell us as otherwise it will be assumed you are using (fully) supported software. If you are using SQL Server 2014 or prior I suggest that you either upgrade, or you do this outside of SQL Server, as 2014 and prior have no JSON support.