Dip*_*Dip 1 sql sql-server json sql-server-2016 ndjson
如何在 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
Run Code Online (Sandbox Code Playgroud)
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"}}
]
Run Code Online (Sandbox Code Playgroud)
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"}}
Run Code Online (Sandbox Code Playgroud)
您可以使用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
Run Code Online (Sandbox Code Playgroud)
其中文件csv.fmt包含:
13.0
1
1 SQLCHAR 0 0 "\r\n" 1 json ""
Run Code Online (Sandbox Code Playgroud)
结果:
有关此MSDN 博客的更多信息