I am trying to remove nested property from a json using json_modify function and i am getting unexpected result.
declare @json as varchar(200) = '{"Location":{"Type":"town"},"Nearby":{"Radius":100000,"Latitude":38,"Longitude":10}}'
select json_modify(@json, '$.Location.Type', null)
I would expect to get:
{"Nearby":{"Radius":100000,"Latitude":38,"Longitude":10}}
but instead i get:
{"Location":{"Latitude":38,"Longitude":10}}
which does look completely wrong. Is there an explanation of such a behavior?
JSON_MODIFYshould be just$.Location, as you want to remove the entireLocationentity.{"Nearby":{"Radius":100000,"Latitude":38,"Longitude":10}},. I would more likely expect{"Location":"","Nearby":{"Radius":100000,"Latitude":38,"Longitude":10}}if you're just removing theTypeentity.strict, theTypeproperty is set tonull(as expected) andNearbyis retained. It looks like an edge case (or, dare I say it, bug) where the (indirect) removal of the parent object ends up shuffling the remaining properties around entirely, "as if" the second object had been deleted too.select json_modify('{"a":{"x":1}, "b":{"y":1}}', '$.a.x', null)works correctly,select json_modify('{"a":{"x":1}, "b":{"y":1,"z":2}}', '$.a.x', null)does not. I'd love to have a look at the code that manages to muck this up, it must be an interesting mistake. :P