SQL Server JSON - Check if field exists by name

3lxsmp7m  于 2023-03-17  发布在  其他
关注(0)|答案(1)|浏览(110)

I have a field name stored in the database. I would like to check if this field exists in my JSON in Angular 13 app. In XML I would do this: myXmlDoc.SelectSingleNode("/My_RootNode/id") Is it possible with JSON?

zbq4xfa0

zbq4xfa01#

You can do this using JSON_VALUE directly in SQL Server.

SELECT *
FROM YourTable
WHERE JSON_VALUE(YourJson, '$.My_RootNode.id') IS NOT NULL;

If the id property is actually an object or array (as opposed to scalar) then use JSON_QUERY

WHERE JSON_QUERY(YourJson, '$.My_RootNode.id') IS NOT NULL;

In SQL Server 2022 you can use JSON_PATH_EXISTS for both cases

WHERE JSON_PATH_EXISTS(YourJson, '$.My_RootNode.id') = 1;

相关问题