SQL Server 2016 get JSONObject from JSONArray using JSONPath - sql

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
If we execute above query it is returning Customers Address object.
AddressObject
{"State":"KA","Country":"India"}
I want get single Customer object according to property like
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"}
can we do this in SQL server 2016?

Related

Create JSON string using variable

I am trying to get following JSON string:
[{
"Name": "John",
"AccountType": 1
},
{
"Name": "Steven",
"AccountType": 1
}
]
I know that AccountType will be always 1 and I have string variable in following format "John;Steven;Brian;Mike"
I was trying to build this JSON using XML PATH and splitToTable function but with no success. How can I achieve it?
Thanks in advance.
In SQL Server 2016 Microsoft added some useful JSON functions. So this can be achieved very easily:
DECLARE #Variable nvarchar(max) = 'John;Steven;Brian;Mike';
SELECT [value] as Name, 1 as AccountType FROM STRING_SPLIT(#Variable,';')
FOR JSON PATH
More info about this functions here - https://learn.microsoft.com/en-us/sql/relational-databases/json/json-data-sql-server?view=sql-server-ver15
You can use string_split() and for json:
declare #str nvarchar(max) = 'John;Steven;Brian;Mike';
select *
from (
select value as [Name], 1 as [AccountType]
from string_split(#str, ',')
) t
for json auto;

Select part from the values in SQL

I have such problem/question. I am trying to extract data from database, but only part. The real example is:
{
"email":"bla#gmail.com",
"addinfo":{
"invoice_id":"1F5FspmpyfQ"
},
"cardholder":"blabla",
"masked_pan":"123456XXXXXX1234"
}
I need to receive only 1F5FspmpyfQ, all between {"invoice_id": " and "},.
You can use Postgres' JSON functions:
select the_column::jsonb -> 'addinfo' ->> 'invoice_id' as invoice_id
from the_table;
-> returns a json object with the specified key and ->> returns the key's value as a text (rather than jsonb)
You can use JSON functions in SQL Server, but JSON support was not introduced until SQL Server 2016:
DECLARE #json NVARCHAR(MAX);
SET #json = '{
"email":"blablabla",
"addinfo":{
"invoice_id":"1F5FspmpyfQ"
},
"cardholder":"bla bla",
"masked_pan":"12345XXXXXX1234"
}';
SELECT JSON_VALUE(#json, '$.addinfo.invoice_id');

Convert a mssql openjson array type value result to a table?

I have a json object in my Microsoft (MS) SQL Server query. This JSON object does have one value, which is an array of strings.
--this variable holds my JSON object with a value of array type.
declare #json nvarchar(max) = N'{
"value": [
"tapiwanashe",
"robert",
"emmerson",
"ruwimbo",
"takudzwa",
"munyaradzi"
]
}'
My goal is to write a SQL query using the supported MS SQL Server JSON functions that produces a table with one column and six rows of the values in the JSON object value array above.
I have tried to run the JSON_QUERY and the OPENJSON functions. However, both of the two functions return an array of strings as the output. I would like to have a result with one column and six rows.
select JSON_QUERY(#json, '$.value')
select [value] from OPENJSON(#json)
The result I am getting is:
value
---------------
[
"tapiwanashe",
"robert",
"emmerson",
"ruwimbo",
"takudzwa",
"munyaradzi"
]
However, the result I am expecting to get looks like this:
value
-----------
tapiwanashe
robert
emmerson
ruwimbo
takudzwa
munyaradzi
The result must preserve the order on which the values appear in the value array.
Like this:
declare #json nvarchar(max) = N'{
"value": [
"tapiwanashe",
"robert",
"emmerson",
"ruwimbo",
"takudzwa",
"munyaradzi"
]
}'
select value
from openjson(#json,'$.value')
order by [key]
outputs
value
----------
tapiwanashe
robert
emmerson
ruwimbo
takudzwa
munyaradzi

I need all value from JSON array

I have a table in which I have JSON data and the field type is NVARCHAR(4000)
[
{"number":1,"booked":0},
{"number":2,"booked":0},
{"number":3,"booked":0},
{"number":4,"booked":1},
{"number":5,"booked":0},
{"number":6,"booked":0},
{"number":7,"booked":0},
{"number":8,"booked":0}
]
I want to query on this field of array, and want the output that Number of booked is 1 and not booked are 7.
I have used JSON_VALUE(), JSON_QUERY() functions but not getting at the point.
I also want that Number:4 is booked.
I am using SQL Server 2016
Hi if i understand all what your're trying todo, thoses example can respond :
DECLARE #json NVARCHAR(MAX)
SET #json =
N'[
{"number":1,"booked":0},
{"number":2,"booked":0},
{"number":3,"booked":0},
{"number":4,"booked":1},
{"number":5,"booked":0},
{"number":6,"booked":0},
{"number":7,"booked":0},
{"number":8,"booked":0}
]'
SELECT number, booked
FROM OPENJSON(#json)
WITH (number int 'strict $.number', booked int 'strict $.booked')
WHERE booked = 1
In futur propose please provide some data and excepted output and query what your're trying .
[
{"number":1,"booked":0},
{"number":2,"booked":0},
{"number":3,"booked":0},
{"number":4,"booked":1},
{"number":5,"booked":0},
{"number":6,"booked":0},
{"number":7,"booked":0},
{"number":8,"booked":0}
]
Select query:
SELECT
COUNT(JSON_VALUE(jsonInfo,'$.booked'))
OVER(PARTITION BY JSON_VALUE(jsonInfo,'$.booked'))
FROM table
GROUP BY JSON_VALUE(jsonInfo,'$.booked')
ORDER BY JSON_VALUE(jsonInfo,'$.booked')

SQL Server 2017 Selecting JSON embedded within a JSON field

In SQL Server 2017, I'd like to "SELECT" a JSON object embedded within another as a string so we can store/process them later.
eg JSON:
[
{"key1":"value1",
"level2_Obj":{"key2":"value12"}
},
{"key1":"value2",
"level2_Obj":{"key22":"value22"}
},
]
From above JSON, I'd like to SELECT whole of the level2Obj JSON object, see below for what I'd like to see the "selection" result.
value1 |{"key2" :"value12"}
value2 |{"key22":"value22"}
I tried below with no luck:
SELECT * FROM
OPENJSON(#json,'$."data1"')
WITH(
[key1] nvarchar(50),
[embedded_json] nvarchar(max) '$."level2Obj"'
) AS DAP
Can some one please help how I select the contents of the 2nd level JSON object as a string?
The idea is to Write 1st level JSON properties into individual cells and rest of JSON levels into a single column of type nvarchar(max) (i.e whole of sub-level JSON object into a single column as a string for further processing in later stages).
Good day,
Firstly, Your JSON text is not properly formatted. There is extra comma after the last object in the array. I will remove this extra comma for the sake of the answer, but if this is the format you have then first step will be to clear the text and make sure that is is well formatted.
Please check if this solve your needs:
declare #json nvarchar(MAX) = '
[
{
"key1":"value1",
"level2_Obj":{"key2":"value12"}
}
,
{
"key1":"value2",
"level2_Obj":{"key22":"value22"}
}
]
'
SELECT JSON_VALUE (t1.[value], '$."key1"'), JSON_QUERY (t1.[value], '$."level2_Obj"')
FROM OPENJSON(#json,'$') t1