[英]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"
},
I want all the names and quantity in 2 column, I don't know how to write query from the same.我想要 2 列中的所有名称和数量,我不知道如何编写查询。
So using a sub-select to "make the data" and then using PARSE_JSON and FLATTEN we get:所以使用子选择来“制作数据”,然后使用 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;
which gives:这使:
NAME![]() |
QUANITIY![]() |
---|---|
A![]() |
1 ![]() |
b ![]() |
1 ![]() |
C ![]() |
2 ![]() |
D ![]() |
2 ![]() |
another way to see the exact same code but with the JSON moved to a CTE (so it looks like a normal table)另一种查看完全相同代码的方法,但将 JSON 移至 CTE(因此它看起来像普通表)
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;
Here is my approach for your problem:这是我解决您问题的方法:
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 $1:name::string, $1:quantity::string from test_json;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.