1

如何在 SQL Server 2016 中打开 ndJSON 格式?我可以使用 JSON 格式打开,但对如何使用 ndJSON 进行操作一无所知。

SQL Server 中是否有特定的函数可以执行此操作,或者是否有其他方法?

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"}}
4

1 回答 1

5

您可以使用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 ""

结果:

在此处输入图像描述

有关此MSDN 博客的更多信息

于 2018-05-16T10:56:07.020 回答