OPENJSON function parses collection of JSON objects and returns values from JSON text as set of rows. If the values in input object are nested, additional mapping parameter can be specified in each column in WITH clause:
declare @json nvarchar(4000) = N'[
{"data":{"num":"SO43659","date":"2011-05-31T00:00:00"},"info":{"customer":"MSFT","Price":59.99,"qty":1}},
{"data":{"number":"SO43661","date":"2011-06-01T00:00:00"},"info":{"customer":"Nokia","Price":24.99,"qty":3}}
]'
SELECT *
FROM OPENJSON (@json)
WITH (
Number varchar(200) '$.data.num',
Date datetime '$.data.date',
Customer varchar(200) '$.info.customer',
Quantity int '$.info.qty',
)
In the WITH clause is specified return schema of OPENJSON function. After the type is specified path to the JSON nodes where returned value should be found. Keys in the JSON objects are fetched by these paths. Values are automatically converted into specified types.
Number | Date | Customer | Quantity |
---|---|---|---|
SO43659 | 2011-05-31T00:00:00 | MSFT | 1 |
SO43661 | 2011-06-01T00:00:00 | Nokia | 3 |