I have this data in a table:
{ "keys": [ "00170000007u8uJ" ] }
I need to extract only the value 00170000007u8uJ
in a SQL Server 2016 select query.
I have this data in a table:
{ "keys": [ "00170000007u8uJ" ] }
I need to extract only the value 00170000007u8uJ
in a SQL Server 2016 select query.
This should do it
declare @json nvarchar(max)=N'{ "keys": [ "00170000007u8uJ" ] }';
select [value] from openjson(json_query(@json, N'$.keys'));
If the JSON is in a table then like this
create table #temp_json(json_col nvarchar(max));
insert #temp_json(json_col) values (N'{ "keys": [ "00170000007u8uJ" ] }');
select [value]
from
#temp_json tj
cross apply
openjson(json_query(tj.json_col, N'$.keys'));
#temp
in the FROM
clause. Note that the query will return multiple rows when the array has multiple keys. Also, the query will fail if you pass invalid JSON (e.g. an XML value).
Commented
Aug 14, 2020 at 14:13