英文:
How to get a @ as a column name and $ as value in Snowflake Sql
问题
I received json unstructured data that I attempted to parse but was unable to do so using json_parse.
我收到了不规则的JSON数据,尝试使用json_parse解析,但无法成功。
I have an entire column for this kind of data, from which I'm providing a sample of the data and the results I need. Could you please advise me on a strategy?
我有一个完整的列用于这种类型的数据,我提供了数据的样本以及我需要的结果。请问您能否就策略给我提供建议?
Sample Data:-
样本数据:-
[
{
"$": "5.1.0.18",
"@": "agon"
},
{
"$": "199891e7-d75c",
"@": "aged"
},
{
"$": "SVE",
"@": "sta"
},
{
"$": "230Z",
"@": "lasIn"
},
{
"$": "69.11",
"@": "conom"
},
{
"$": "Invplete",
"]@": "chus"
},
{
"$": "Win",
"@": "pla"
},
{
"$": "A_PM,AGENT_SCA,ANT_VM",
"@": "adMe"
},
{
"$": [
{
"$": "VULIGS-M-2.5.77-3",
"@": "vm"
},
{
"$": "VULNS-SA-2.5.767-2",
"@": "sca"
}
],
"@": "manstVeon",
"sa": 1,
"vrm": 0
},
{
"$": [
{
"$": 3905,
"@": "id"
},
{
"$": "Sta Cnt - No Auo Uate",
"@": "name"
}
],
"@": "agtion",
"id": 0,
"name": 1
},
{
"$": [
{
"$": "abe7-0aa7141fe2e2",
"@": "actId"
},
{
"$": "IT t",
"@": "title"
}
],
"@": "acey",
"acId": 0,
"title": 1
}
]
made changes in the code section
在代码部分进行了更改
英文:
I received json unstructured data that I attempted to parse but was unable to do so using json_parse.
I have an entire column for this kind of data, from which I'm providing a sample of the data and the results I need. Could you please advise me on a strategy?
Sample Data:-
[
{
"$": "5.1.0.18",
"@": "agon"
},
{
"$": "199891e7-d75c",
"@": "aged"
},
{
"$": "SVE",
"@": "sta"
},
{
"$": "230Z",
"@": "lasIn"
},
{
"$": "69.11",
"@": "conom"
},
{
"$": "Invplete",
"]@": "chus"
},
{
"$": "Win",
"@": "pla"
},
{
"$": "A_PM,AGENT_SCA,ANT_VM",
"@": "adMe"
},
{
"$": [
{
"$": "VULIGS-M-2.5.77-3",
"@": "vm"
},
{
"$": "VULNS-SA-2.5.767-2",
"@": "sca"
}
],
"@": "manstVeon",
"sa": 1,
"vrm": 0
},
{
"$": [
{
"$": 3905,
"@": "id"
},
{
"$": "Sta Cnt - No Auo Uate",
"@": "name"
}
],
"@": "agtion",
"id": 0,
"name": 1
},
{
"$": [
{
"$": "abe7-0aa7141fe2e2",
"@": "actId"
},
{
"$": "IT t",
"@": "title"
}
],
"@": "acey",
"acId": 0,
"title": 1
}
]
答案1
得分: 0
以下是已翻译的内容:
你可以使用以下语法提取具有特殊字符名称的JSON属性:
GET(JSON, '@')::string,
GET(JSON, '$')::string
对于你的查询,看起来你还需要处理展开和透视,这会得到类似以下的结果(作为一个较小的示例,因为我不完全确定你希望得到的输出如何):
with json_cte as (
select parse_json('[
{
"$": "5.1.0.18",
"@": "agon"
},
{
"$": "199891e7-d75c",
"@": "aged"
}]'
) as json_array
)
, attributes (
select
GET(a.value, '@')::string attr,
GET(a.value, '$')::string val
from json_cte,
lateral flatten(input => json_array) a
)
select *
from attributes
pivot(max(val) for attr in ('agon', 'aged'));
英文:
You can use this syntax to extract JSON attributes that have special characters in their names:
GET(JSON, '@')::string,
GET(JSON, '$')::string
For your query it looks like you're also needing to deal with flattening and pivoting, which gives you something like this (reduced to a smaller set as an example, since I'm not 100% sure how you're expecting to get to your requested output)
with json_cte as (
select parse_json('[
{
"$": "5.1.0.18",
"@": "agon"
},
{
"$": "199891e7-d75c",
"@": "aged"
}]'
) as json_array
)
, attributes (
select
GET(a.value, '@')::string attr,
GET(a.value, '$')::string val
from json_cte,
lateral flatten(input => json_array) a
)
select *
from attributes
pivot(max(val) for attr in ('agon', 'aged'));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论