将 JSON 数据从 JSON 文件导入 SQL Server
我有一个像这样的JSON文件:
{"Rows":
[{"a":"abc"
,"b":"def"
,"c":"x"
,"d":"yuy"
,"e":"aaa"
,"f":"bcb"
,"g":"wer"
,"h":"www"
,"i":123
,"j":456.0
,"k":"12/1/1999 12:02:49 AM"
,"l":1.000
,"m":52.10
,"n":12.990
,"o":8.40
,"p":3
,"q":8.37
,"r":63.0
,"s":7.2
,"t":"1-dfbaaaf"
,"u":"dppp-9c1"
,"v":"12/28/2066 6:02:48 AM"
,"w":"2824865"
,"x":"123"
,"y":"2-1c-847a-06e27"}
]}
我尝试了以下代码,它为我提供了所有null
行:
DECLARE @JSON varchar(max)
SELECT @JSON = BulkColumn
FROM OPENROWSET (BULK 'C:\f1.json', SINGLE_CLOB) AS j
SELECT *
FROM OPENJSON (@JSON)
WITH (
[a] [nvarchar](50) ,
[b] [nvarchar](50) ,
[c] [nvarchar](50) ,
[d] [int] ,
[e] [nvarchar](50) ,
[f] [nvarchar](50) ,
[g] [nvarchar](50) ,
[h] [nvarchar](50) ,
[i] [nvarchar](50) ,
[j] [nvarchar](50),
[k] [datetime2](7) ,
[l] [nvarchar](50) ,
[m] [nvarchar](50) ,
[n] [nvarchar](50) ,
[o] [nvarchar](50) ,
[p] [nvarchar](50),
[q] [nvarchar](50) ,
[r] [nvarchar](50) ,
[s] [nvarchar](50) ,
[t] [nvarchar](50) ,
[u] [nvarchar](50),
[v] [datetime2](7) ,
[w] [nvarchar](50),
x [nvarchar](50),
y [nvarchar](500)
)
我还尝试了Cross Applion
,但我只能获得nulls。
SELECT *
INTO dbo.Test_JSON
FROM OPENROWSET (BULK 'C\f1.json', Single_CLOB) AS import;
SELECT b, c
FROM dbo.Test_JSON
CROSS APPLY OPENJSON (BUlkColumn)
WITH
(
[Rows] nvarchar(max) AS json,
a uniqueidentifier,
b varchar(50),
c varchar(50)
);
任何帮助都将受到极大的赞赏,而且我还需要为Azure数据仓库提供解决方案,我还想实现SQL Server和Azure Data Data Warehouse的解决方案。
I have a JSON file which is something like this:
{"Rows":
[{"a":"abc"
,"b":"def"
,"c":"x"
,"d":"yuy"
,"e":"aaa"
,"f":"bcb"
,"g":"wer"
,"h":"www"
,"i":123
,"j":456.0
,"k":"12/1/1999 12:02:49 AM"
,"l":1.000
,"m":52.10
,"n":12.990
,"o":8.40
,"p":3
,"q":8.37
,"r":63.0
,"s":7.2
,"t":"1-dfbaaaf"
,"u":"dppp-9c1"
,"v":"12/28/2066 6:02:48 AM"
,"w":"2824865"
,"x":"123"
,"y":"2-1c-847a-06e27"}
]}
I tried this below code and it gives me all null
rows:
DECLARE @JSON varchar(max)
SELECT @JSON = BulkColumn
FROM OPENROWSET (BULK 'C:\f1.json', SINGLE_CLOB) AS j
SELECT *
FROM OPENJSON (@JSON)
WITH (
[a] [nvarchar](50) ,
[b] [nvarchar](50) ,
[c] [nvarchar](50) ,
[d] [int] ,
[e] [nvarchar](50) ,
[f] [nvarchar](50) ,
[g] [nvarchar](50) ,
[h] [nvarchar](50) ,
[i] [nvarchar](50) ,
[j] [nvarchar](50),
[k] [datetime2](7) ,
[l] [nvarchar](50) ,
[m] [nvarchar](50) ,
[n] [nvarchar](50) ,
[o] [nvarchar](50) ,
[p] [nvarchar](50),
[q] [nvarchar](50) ,
[r] [nvarchar](50) ,
[s] [nvarchar](50) ,
[t] [nvarchar](50) ,
[u] [nvarchar](50),
[v] [datetime2](7) ,
[w] [nvarchar](50),
x [nvarchar](50),
y [nvarchar](500)
)
I also tried CROSS APPLY
, but I get nulls only.
SELECT *
INTO dbo.Test_JSON
FROM OPENROWSET (BULK 'C\f1.json', Single_CLOB) AS import;
SELECT b, c
FROM dbo.Test_JSON
CROSS APPLY OPENJSON (BUlkColumn)
WITH
(
[Rows] nvarchar(max) AS json,
a uniqueidentifier,
b varchar(50),
c varchar(50)
);
Any help is greatly appreciated and also I need the solution for Azure Data Warehouse as well I want to implement the solution for both SQL Server and Azure Data Warehouse.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您只需要在
OPENJSON()
调用中添加一个path
(在您的情况下为$.Rows
):另外,请注意,实际的示例数据并不与显式架构(
WITH
子句)中的列数据类型不匹配。You simply need a
path
($.Rows
in your case) in yourOPENJSON()
call:Also, note that the actual sample data doesn't match the columns data types in the explicit schema (the
WITH
clause).尝试外部应用并注意行的 AS JSON 的使用。我只是恢复第一个属性。
Try an Outer Apply and note the use of AS JSON for Rows. I'm just bringing the first attribute back.