Импорт вложенных пар имя-значение из json в SQL Server - PullRequest
0 голосов
/ 14 февраля 2019

Я импортирую файл 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 ":" Calories "," value ":" 25 "}]}, {" id ": 23," fruit":" Banana "," values ​​": [{" name ":" Color "," value ":" Yellow "}, {" name ":" Weight "," value ":" 30 "}, {" name":" Калории "," ценность ":" 250 "}]}]

1 Ответ

0 голосов
/ 14 февраля 2019

Вы можете попробовать следующий подход, который вернет полные данные:

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
...