我正在将一个 json 文件导入 SQL 2016,它在嵌套值结构中具有一些嵌套的名称-值对。这是我遇到问题的这些对中的值,例如 .
{ "name": "Colour", "value": "Orange" }, { "name": "Calories", "value": "25" }
sql:
Declare @JSON varchar(max)
SELECT @JSON = BulkColumn
FROM OPENROWSET (BULK 'C:\temp\fruit.json', SINGLE_CLOB) as j
If (ISJSON(@JSON)=1) BEGIN
Select * from openjson ( @JSON )
WITH(
id int,
fruit varchar(20),
Colour varchar(20) '$.values.Colour',
Weight int '$.values.Weight'
) as Orders
END
ELSE
Select 'JSON is invalid!'
其结果是:
id fruit Colour Weight
1 orange NULL NULL
23 Banana NULL NULL
完整(测试)数据..
[ { "id": 1, "fruit": "orange", "values": [ { "name": "Color", "value": "Orange" }, { "name": "Weight", "value ": "16" }, { "name": "卡路里", "value": "25" } ] }, { "id": 23, "fruit": "Banana", "values": [ { "name “:“颜色”,“价值”:“黄色”},{“名称”:“体重”,“价值”:“30”},{“名称”:“卡路里”,“价值”:“250”} ] } ]