简体   繁体   中英

Read JSON data from CSV in Synapse Azure

I have a CSV with some columns, one of them with JSON content. Can I query this JSON column with special handling?

An example below: ei

My goal is to run a query ( openrowset documentation ) and get output similar to this.

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. First, I created variable with nvarchar(max) . set the select Querys value to it.

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:

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:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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