SQL Server 2016使用JSONPath从JSONArray获取JSONObject

时间:2016-07-14 09:43:15

标签: sql sql-server

DECLARE @json_str NVARCHAR(MAX) = 
 '{"Customers":
    [{"Id":1,"Name":"Basavaraj",
            "Address":{"State":"KA","Country":"India"}},
     {"Id":2,"Name":"Kalpana",
            "Address":{"State":"NY","Country":"United State"}}
    ]
 }'
 SELECT JSON_QUERY(@json_str,'$.Customers[0].Address') AddressObject

如果我们执行上述查询,则返回Customers Address对象。

AddressObject

{ “状态”: “KA”, “国家”: “印”}

我想根据属性

获取单个Customer对象
DECLARE @json_str NVARCHAR(MAX) = 
     '{"Customers":
        [{"Id":1,"Name":"Basavaraj",
                "Address":{"State":"KA","Country":"India"}},
         {"Id":2,"Name":"Kalpana",
                "Address":{"State":"NY","Country":"United State"}}
        ]
     }'
     SELECT JSON_QUERY(@json_str,'$.Customers[].State') AddressObject where AddressObject="NY"

AddressObject

{“State”:“NY”,“Country”:“United State”}

我们可以在SQL Server 2016中执行此操作吗?

0 个答案:

没有答案