在 SQL Server 2016 中打开 ndJSON 格式

Opening ndJSON format in SQL Server 2016

如何在 SQL Server 2016 中打开 ndJSON 格式?我可以用 JSON 格式打开,但不知道如何用 ndJSON.

SQL 服务器中是否有特定功能可以执行此操作,或者是否有其他方法?

Declare @JSON varchar(max)

SELECT @JSON = BulkColumn
FROM OPENROWSET (BULK 'C:\examplepath\filename.JSON', SINGLE_CLOB) as j


Select * FROM OPENJSON(@JSON)
With (House varchar(50), 
      Car varchar(4000) '$.Attributes.Car',
      Door varchar(4000) '$.Attributes.Door',
      Bathroom varchar(4000) '$.Attributes.Bathroom' ,
      Basement varchar(4000) '$.Attributes.Basement' ,
      Attic varchar(4000) '$.Attributes.Attic'
      ) as Dataset
Go

JSON格式:

[

{"House":"Blue","Attributes":{"Car":"Camry","Door":"Small","Bathroom":"Medium","Basement":"Dark","Attic":"1"}},
{"House":"Red","Attributes":{"Car":"Thunderbird","Door":"Large","Bathroom":"Small","Basement":"Light","Attic":"4"}}

]

ndJSON格式:

{"House":"Blue","Attributes":{"Car":"Camry","Door":"Small","Bathroom":"Medium","Basement":"Dark","Attic":"1"}}
{"House":"Red","Attributes":{"Car":"Thunderbird","Door":"Large","Bathroom":"Small","Basement":"Light","Attic":"4"}}

您可以使用 FORMATFILE 子句:

SELECT  House, Car, Door, Bathroom, Basement, Attic   
FROM OPENROWSET (BULK 'D:\ndjson\ndjson.json', FORMATFILE= 'D:\ndjson\csv.fmt' ) as j
  CROSS APPLY OPENJSON(json) With 
  (   House    varchar(50), 
      Car      varchar(4000) '$.Attributes.Car',
      Door     varchar(4000) '$.Attributes.Door',
      Bathroom varchar(4000) '$.Attributes.Bathroom' ,
      Basement varchar(4000) '$.Attributes.Basement' ,
      Attic    varchar(4000) '$.Attributes.Attic'
  ) as Dataset

文件 csv.fmt 包含:

13.0
1
1 SQLCHAR 0 0 "\r\n" 1 json ""

结果:

有关 this MSDN 博客的更多信息