无法将Json解析回SQL Server中的表

时间:2019-04-15 14:36:21

标签: json sql-server parsing

我已经在SQL Server中使用FOR JSON PATH生成了一个Json文档,但是,当我尝试将其解析回表时,我只会得到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 个答案:

答案 0 :(得分:1)

赞:

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'
      )

答案 1 :(得分:1)

您需要在OPENJSON中指定一个JSON Path表达式,以选择所需的数据。 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