Невозможно разобрать Json обратно в таблицу в SQL Server - PullRequest
0 голосов
/ 15 апреля 2019

Я сгенерировал документ Json, используя FOR JSON PATH в SQL Server, однако, когда я пытаюсь разобрать его обратно в таблицу, я просто получаю NULL.

SELECT * 
FROM OPENJSON('{"Customer":[{"CustomerID":10021,"CustomerNumber":"ALFKI","Customer":"Alfreds Futterkiste","City":"Berlin","OrderID":11060,"UnitPrice":45.6000,"ProductsOnOrder":15,"ShipCity":"Berlin"},{"CustomerID":10021,"CustomerNumber":"ALFKI","Customer":"Alfreds Futterkiste","City":"Berlin","OrderID":11061,"UnitPrice":18.0000,"ProductsOnOrder":21,"ShipCity":"Berlin"},{"CustomerID":10021,"CustomerNumber":"ALFKI","Customer":"Alfreds Futterkiste","City":"Berlin","OrderID":11062,"UnitPrice":12.0000,"ProductsOnOrder":2,"ShipCity":"Berlin"},{"CustomerID":10021,"CustomerNumber":"ALFKI","Customer":"Alfreds Futterkiste","City":"Berlin","OrderID":11187,"UnitPrice":43.9000,"ProductsOnOrder":20,"ShipCity":"Berlin"},{"CustomerID":10021,"CustomerNumber":"ALFKI","Customer":"Alfreds Futterkiste","City":"Berlin","OrderID":11217,"UnitPrice":10.0000,"ProductsOnOrder":6,"ShipCity":"Berlin"},{"CustomerID":10021,"CustomerNumber":"ALFKI","Customer":"Alfreds Futterkiste","City":"Berlin","OrderID":11218,"UnitPrice":18.0000,"ProductsOnOrder":15,"ShipCity":"Berlin"},{"CustomerID":10021,"CustomerNumber":"ALFKI","Customer":"Alfreds Futterkiste","City":"Berlin","OrderID":11554,"UnitPrice":55.0000,"ProductsOnOrder":15,"ShipCity":"Berlin"},{"CustomerID":10021,"CustomerNumber":"ALFKI","Customer":"Alfreds Futterkiste","City":"Berlin","OrderID":11555,"UnitPrice":13.0000,"ProductsOnOrder":2,"ShipCity":"Berlin"},{"CustomerID":10021,"CustomerNumber":"ALFKI","Customer":"Alfreds Futterkiste","City":"Berlin","OrderID":11846,"UnitPrice":25.0000,"ProductsOnOrder":16,"ShipCity":"Berlin"},{"CustomerID":10021,"CustomerNumber":"ALFKI","Customer":"Alfreds Futterkiste","City":"Berlin","OrderID":11847,"UnitPrice":45.6000,"ProductsOnOrder":2,"ShipCity":"Berlin"},{"CustomerID":10021,"CustomerNumber":"ALFKI","Customer":"Alfreds Futterkiste","City":"Berlin","OrderID":11992,"UnitPrice":13.2500,"ProductsOnOrder":40,"ShipCity":"Berlin"},{"CustomerID":10021,"CustomerNumber":"ALFKI","Customer":"Alfreds Futterkiste","City":"Berlin","OrderID":11993,"UnitPrice":21.5000,"ProductsOnOrder":20,"ShipCity":"Berlin"}]}')
WITH
(
    CustomerID NVARCHAR(255) '$.Customer'
)

Ответы [ 2 ]

1 голос
/ 15 апреля 2019

Вам необходимо указать выражение JSON Path в OPENJSON, которое выбирает нужные данные.WITH применяется к результату выражения JSON Path.

Без него корневой объект анализируется и возвращается одна строка для каждого атрибута.В данном случае это Customer с массивом значений.Если вы использовали OPENJSON без пути, вы получите:

key       value              type
Customer  [{"Customer".....  4

Если корневой объект содержит другое свойство, например "moo":123, оно будет возвращено в другой строке:

key       value              type
Customer  [{"Customer".....  4
moo       123                2

Чтобы выбрать содержимое свойства Customer, необходимо использовать путь $.Customer.Это вернет элементы массива:

SELECT *
FROM OPENJSON(@json,'$.Customer')

Results
-------

0   {"CustomerID":10021,"CustomerN  5
1   {"CustomerID":10021,"CustomerN  5

С теперь применяется к элементам массива и:

SELECT *
FROM OPENJSON(@json,'$.Customer')
WITH(
    CustomerID NVARCHAR(255) '$.CustomerID',
    CustomerNumber NVARCHAR(255) '$.CustomerNumber',
    Customer NVARCHAR(255) '$.Customer'
)

Вернет

CustomerID  CustomerNumber  Customer
10021       ALFKI           Alfreds Futterkiste
10021       ALFKI           Alfreds Futterkiste
10021       ALFKI           Alfreds Futterkiste
1 голос
/ 15 апреля 2019

Как это:

declare @json nvarchar(max) = N'{"Customer":[{"CustomerID":10021,"CustomerNumber":"ALFKI","Customer":"Alfreds Futterkiste","City":"Berlin","OrderID":11060,"UnitPrice":45.6000,"ProductsOnOrder":15,"ShipCity":"Berlin"},{"CustomerID":10021,"CustomerNumber":"ALFKI","Customer":"Alfreds Futterkiste","City":"Berlin","OrderID":11061,"UnitPrice":18.0000,"ProductsOnOrder":21,"ShipCity":"Berlin"},{"CustomerID":10021,"CustomerNumber":"ALFKI","Customer":"Alfreds Futterkiste","City":"Berlin","OrderID":11062,"UnitPrice":12.0000,"ProductsOnOrder":2,"ShipCity":"Berlin"},{"CustomerID":10021,"CustomerNumber":"ALFKI","Customer":"Alfreds Futterkiste","City":"Berlin","OrderID":11187,"UnitPrice":43.9000,"ProductsOnOrder":20,"ShipCity":"Berlin"},{"CustomerID":10021,"CustomerNumber":"ALFKI","Customer":"Alfreds Futterkiste","City":"Berlin","OrderID":11217,"UnitPrice":10.0000,"ProductsOnOrder":6,"ShipCity":"Berlin"},{"CustomerID":10021,"CustomerNumber":"ALFKI","Customer":"Alfreds Futterkiste","City":"Berlin","OrderID":11218,"UnitPrice":18.0000,"ProductsOnOrder":15,"ShipCity":"Berlin"},{"CustomerID":10021,"CustomerNumber":"ALFKI","Customer":"Alfreds Futterkiste","City":"Berlin","OrderID":11554,"UnitPrice":55.0000,"ProductsOnOrder":15,"ShipCity":"Berlin"},{"CustomerID":10021,"CustomerNumber":"ALFKI","Customer":"Alfreds Futterkiste","City":"Berlin","OrderID":11555,"UnitPrice":13.0000,"ProductsOnOrder":2,"ShipCity":"Berlin"},{"CustomerID":10021,"CustomerNumber":"ALFKI","Customer":"Alfreds Futterkiste","City":"Berlin","OrderID":11846,"UnitPrice":25.0000,"ProductsOnOrder":16,"ShipCity":"Berlin"},{"CustomerID":10021,"CustomerNumber":"ALFKI","Customer":"Alfreds Futterkiste","City":"Berlin","OrderID":11847,"UnitPrice":45.6000,"ProductsOnOrder":2,"ShipCity":"Berlin"},{"CustomerID":10021,"CustomerNumber":"ALFKI","Customer":"Alfreds Futterkiste","City":"Berlin","OrderID":11992,"UnitPrice":13.2500,"ProductsOnOrder":40,"ShipCity":"Berlin"},{"CustomerID":10021,"CustomerNumber":"ALFKI","Customer":"Alfreds Futterkiste","City":"Berlin","OrderID":11993,"UnitPrice":21.5000,"ProductsOnOrder":20,"ShipCity":"Berlin"}]}'


SELECT * 
FROM OPENJSON(@json,'$.Customer') 
      WITH
      (
        CustomerID NVARCHAR(255) '$.CustomerID'
      )
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...