将嵌套的名称/值对从JSON导入SQL Server

时间:2019-02-14 10:35:16

标签: json sql-server sql-server-2016 open-json

我正在将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”,       “值”:[         {“ name”:“ Colour”,“ value”:“ Orange”},         {“ name”:“ Weight”,“ value”:“ 16”},         {“ name”:“卡路里”,“ value”:“ 25”}       ]     },     {       “ id”:23,       “ fruit”:“ Banana”,       “值”:[         {“ name”:“ Colour”,“ value”:“ Yellow”},         {“ name”:“ Weight”,“ value”:“ 30”},         {“ name”:“卡路里”,“ value”:“ 250”}       ]     }   ]

1 个答案:

答案 0 :(得分:2)

您可以尝试下一种方法,该方法将返回完整数据:

DECLARE @json nvarchar(max)
SET @json = N'[ { "id": 1, "fruit": "orange", "values": [ { "name": "Colour", "value": "Orange" }, { "name": "Weight", "value": "16" }, { "name": "Calories", "value": "25" } ] }, { "id": 23, "fruit": "Banana", "values": [ { "name": "Colour", "value": "Yellow" }, { "name": "Weight", "value": "30" }, { "name": "Calories", "value": "250" } ] } ]'

SELECT i.id, i.fruit, v.[name], v.[value]
FROM OPENJSON(@json) 
WITH (
   id int '$.id',
   fruit nvarchar(50) '$.fruit',
   [values] nvarchar(max) '$.values' AS JSON
) AS i
CROSS APPLY (
   SELECT *
   FROM OPENJSON(i.[values])
   WITH (
      [name] nvarchar(max) '$.name',
      [value] nvarchar(max) '$.value'
   )
) v

输出:

id  fruit   name     value
1   orange  Colour   Orange
1   orange  Weight   16
1   orange  Calories 25
23  Banana  Colour   Yellow
23  Banana  Weight   30
23  Banana  Calories 250

如果要输出有关颜色和重量的信息,请尝试以下操作:

DECLARE @json nvarchar(max)
SET @json = N'[ { "id": 1, "fruit": "orange", "values": [ { "name": "Colour", "value": "Orange" }, { "name": "Weight", "value": "16" }, { "name": "Calories", "value": "25" } ] }, { "id": 23, "fruit": "Banana", "values": [ { "name": "Colour", "value": "Yellow" }, { "name": "Weight", "value": "30" }, { "name": "Calories", "value": "250" } ] } ]'

SELECT i.id, i.fruit, v1.[value] AS Colour, v2.[value] AS Weight
FROM OPENJSON(@json) 
WITH (
   id int '$.id',
   fruit nvarchar(50) '$.fruit',
   [values] nvarchar(max) '$.values' AS JSON
) AS i
OUTER APPLY (
   SELECT *
   FROM OPENJSON(i.[values])
   WITH (
      [name] nvarchar(max) '$.name',
      [value] nvarchar(max) '$.value'
   )
   WHERE [name] = 'Colour'
) v1 
OUTER APPLY (
   SELECT *
   FROM OPENJSON(i.[values])
   WITH (
      [name] nvarchar(max) '$.name',
      [value] nvarchar(max) '$.value'
   )
   WHERE [name] = 'Weight'
) v2

输出:

id  fruit   Colour  Weight
1   orange  Orange  16
23  Banana  Yellow  30