[英]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 列吗?
My goal is to run a query ( openrowset documentation ) and get output similar to this.我的目标是运行查询( openrowset 文档)并获得与此类似的 output。
ID ![]() |
Name![]() |
---|---|
0 ![]() |
Valarie Strickland![]() |
1 ![]() |
Mathews Harrison![]() |
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.