简体   繁体   English

在Synapse Azure中从CSV读取JSON数据

[英]Read JSON data from CSV in Synapse Azure

I have a CSV with some columns, one of them with JSON content.我有一个带有一些列的 CSV,其中一个包含 JSON 内容。 Can I query this JSON column with special handling?我可以通过特殊处理查询这个 JSON 列吗?

An example below: ei下面的例子: ei

My goal is to run a query ( openrowset documentation ) and get output similar to this.我的目标是运行查询( openrowset 文档)并获得与此类似的 output。

ID ID Name姓名
0 0 Valarie Strickland瓦拉丽·斯特里克兰
1 1个 Mathews Harrison马修斯·哈里森
2 2个 Cecilia Giles塞西莉亚吉尔斯

I tried to reproduce the same in my environment.我试图在我的环境中重现相同的内容。

My sample data:我的示例数据:

在此处输入图像描述

to convert the column with nested Json in the form of table.以表格的形式转换嵌套Json的列。 First, I created variable with nvarchar(max) .首先,我使用nvarchar(max)创建了变量。 set the select Querys value to it.为其设置 select 查询值。

DECLARE @json nvarchar(max)
SET @json = (SELECT
TOP  100 *
FROM
OPENROWSET(
BULK  'https://dlsg2p.dfs.core.windows.net/fsn2p/jsoncolumn.csv',
FORMAT = 'CSV',
PARSER_VERSION = '2.0',
firstrow=3
) AS [result])

with below statement checking the value is assigned properly to variable.使用以下语句检查值是否已正确分配给变量。

select @json as JSON

Using CROSS APPLY for converting Json to table format:使用 CROSS APPLY 将 Json 转换为表格格式:

SELECT b.id as ID ,b.name  as  Name
FROM
OPENJSON(@json)
WITH
(
friends NVARCHAR(MAX) AS JSON
) AS a
CROSS  APPLY
OPENJSON(a.friends)
WITH
(
id INT,
name  VARCHAR(MAX)
) AS b;

Execution:执行:

在此处输入图像描述

Output: Output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM