如何在Snowflake SQL中将@作为列名,$作为值获取。

huangapple go评论61阅读模式
英文:

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
在代码部分进行了更改

Required Output
所需输出
如何在Snowflake SQL中将@作为列名,$作为值获取。

英文:

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
  }
]

made changes in the code section
Required Output
如何在Snowflake SQL中将@作为列名,$作为值获取。

答案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'));

huangapple
  • 本文由 发表于 2023年6月1日 14:25:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76379179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定