OPENJSON function parses collection of JSON objects and returns values from JSON text as set of rows.
declare @json nvarchar(4000) = N'[
{"Number":"SO43659","Date":"2011-05-31T00:00:00","Customer": "MSFT","Price":59.99,"Quantity":1},
{"Number":"SO43661","Date":"2011-06-01T00:00:00","Customer":"Nokia","Price":24.99,"Quantity":3}
]'
SELECT *
FROM OPENJSON (@json)
WITH (
Number varchar(200),
Date datetime,
Customer varchar(200),
Quantity int
)
In the WITH clause is specified return schema of OPENJSON function. Keys in the JSON objects are fetched by column names. If some key in JSON is not specified in the WITH clause (e.g. Price in this example) it will be ignored. 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 |