如何解析以下 JSON 以检索 snowflake sql 中的项目名称?
How to parse the following JSON to retrieve the name of the item in snowflake sql?
[
{
"itemId": "HWKDVCXKU5",
"name": "A",
"quantity": 1.000000000000000e+00,
"storeId": 1.150192000000000e+06,
"type": "structure"
},
{
"itemId": "FPK81M587X",
"name": "b",
"quantity": 1.000000000000000e+00,
"storeId": 1.150192000000000e+06,
"type": "structure"
},
{
"itemId": "L04WBQON3C",
"name": "C",
"quantity": 2.000000000000000e+00,
"storeId": 1.150192000000000e+06,
"type": "structure"
},
{
"itemId": "ULZFPY2UJN",
"name": "D",
"quantity": 2.000000000000000e+00,
"storeId": 1.150192000000000e+06,
"type": "structure"
},
我想要 2 列中的所有名称和数量,我不知道如何编写相同的查询。
所以使用 sub-select 来“制作数据”,然后使用 PARSE_JSON 和 FLATTEN 我们得到:
SELECT
f.value:name::text as name,
f.value:quantity::float as quanitiy
FROM (
SELECT PARSE_JSON('[
{
"itemId": "HWKDVCXKU5",
"name": "A",
"quantity": 1.000000000000000e+00,
"storeId": 1.150192000000000e+06,
"type": "structure"
},
{
"itemId": "FPK81M587X",
"name": "b",
"quantity": 1.000000000000000e+00,
"storeId": 1.150192000000000e+06,
"type": "structure"
},
{
"itemId": "L04WBQON3C",
"name": "C",
"quantity": 2.000000000000000e+00,
"storeId": 1.150192000000000e+06,
"type": "structure"
},
{
"itemId": "ULZFPY2UJN",
"name": "D",
"quantity": 2.000000000000000e+00,
"storeId": 1.150192000000000e+06,
"type": "structure"
}]') as json
) j
,TABLE(FLATTEN(input=>json)) f;
给出:
NAME
QUANITIY
A
1
b
1
C
2
D
2
查看完全相同代码但 JSON 移至 CTE 的另一种方式(因此它看起来像正常的 table)
WITH data_cte AS (
SELECT PARSE_JSON('[
{
"itemId": "HWKDVCXKU5",
"name": "A",
"quantity": 1.000000000000000e+00,
"storeId": 1.150192000000000e+06,
"type": "structure"
},
{
"itemId": "FPK81M587X",
"name": "b",
"quantity": 1.000000000000000e+00,
"storeId": 1.150192000000000e+06,
"type": "structure"
},
{
"itemId": "L04WBQON3C",
"name": "C",
"quantity": 2.000000000000000e+00,
"storeId": 1.150192000000000e+06,
"type": "structure"
},
{
"itemId": "ULZFPY2UJN",
"name": "D",
"quantity": 2.000000000000000e+00,
"storeId": 1.150192000000000e+06,
"type": "structure"
}]') as json
)
SELECT
f.value:name::text as name,
f.value:quantity::float as quanitiy
FROM data_cte j
,TABLE(FLATTEN(input=>json)) f;
这是我解决你的问题的方法:
create or replace stage my_json_stage;
PUT file:///Users/athakur/Downloads/Chrome Downloads/test.json @my_json_stage auto_compress=false;
create or replace table test_json
( my_value variant);
copy into test_json from @my_json_stage/test.json
file_format = (type = JSON strip_outer_array = true)
ON_Error = Continue;
select * from test_json;
select :name::string, :quantity::string from test_json;
[
{
"itemId": "HWKDVCXKU5",
"name": "A",
"quantity": 1.000000000000000e+00,
"storeId": 1.150192000000000e+06,
"type": "structure"
},
{
"itemId": "FPK81M587X",
"name": "b",
"quantity": 1.000000000000000e+00,
"storeId": 1.150192000000000e+06,
"type": "structure"
},
{
"itemId": "L04WBQON3C",
"name": "C",
"quantity": 2.000000000000000e+00,
"storeId": 1.150192000000000e+06,
"type": "structure"
},
{
"itemId": "ULZFPY2UJN",
"name": "D",
"quantity": 2.000000000000000e+00,
"storeId": 1.150192000000000e+06,
"type": "structure"
},
我想要 2 列中的所有名称和数量,我不知道如何编写相同的查询。
所以使用 sub-select 来“制作数据”,然后使用 PARSE_JSON 和 FLATTEN 我们得到:
SELECT
f.value:name::text as name,
f.value:quantity::float as quanitiy
FROM (
SELECT PARSE_JSON('[
{
"itemId": "HWKDVCXKU5",
"name": "A",
"quantity": 1.000000000000000e+00,
"storeId": 1.150192000000000e+06,
"type": "structure"
},
{
"itemId": "FPK81M587X",
"name": "b",
"quantity": 1.000000000000000e+00,
"storeId": 1.150192000000000e+06,
"type": "structure"
},
{
"itemId": "L04WBQON3C",
"name": "C",
"quantity": 2.000000000000000e+00,
"storeId": 1.150192000000000e+06,
"type": "structure"
},
{
"itemId": "ULZFPY2UJN",
"name": "D",
"quantity": 2.000000000000000e+00,
"storeId": 1.150192000000000e+06,
"type": "structure"
}]') as json
) j
,TABLE(FLATTEN(input=>json)) f;
给出:
NAME | QUANITIY |
---|---|
A | 1 |
b | 1 |
C | 2 |
D | 2 |
查看完全相同代码但 JSON 移至 CTE 的另一种方式(因此它看起来像正常的 table)
WITH data_cte AS (
SELECT PARSE_JSON('[
{
"itemId": "HWKDVCXKU5",
"name": "A",
"quantity": 1.000000000000000e+00,
"storeId": 1.150192000000000e+06,
"type": "structure"
},
{
"itemId": "FPK81M587X",
"name": "b",
"quantity": 1.000000000000000e+00,
"storeId": 1.150192000000000e+06,
"type": "structure"
},
{
"itemId": "L04WBQON3C",
"name": "C",
"quantity": 2.000000000000000e+00,
"storeId": 1.150192000000000e+06,
"type": "structure"
},
{
"itemId": "ULZFPY2UJN",
"name": "D",
"quantity": 2.000000000000000e+00,
"storeId": 1.150192000000000e+06,
"type": "structure"
}]') as json
)
SELECT
f.value:name::text as name,
f.value:quantity::float as quanitiy
FROM data_cte j
,TABLE(FLATTEN(input=>json)) f;
这是我解决你的问题的方法:
create or replace stage my_json_stage;
PUT file:///Users/athakur/Downloads/Chrome Downloads/test.json @my_json_stage auto_compress=false;
create or replace table test_json
( my_value variant);
copy into test_json from @my_json_stage/test.json
file_format = (type = JSON strip_outer_array = true)
ON_Error = Continue;
select * from test_json;
select :name::string, :quantity::string from test_json;