如何在雪花中解析这个json

发布于 2025-01-23 04:53:31 字数 644 浏览 3 评论 0原文

{
  "segmentId": "b204c220-ea8d-4cf4-b579-30eb59a1a2a4",
  "diffFields": [
    {
      "fieldName": "name",
      "valueBefore": null,
      "valueAfter": "new-segment-name"
    },
    {
      "fieldName": "active",
      "valueBefore": null,
      "valueAfter": true
    }
  ]
}

在上面的JSON中,我有一个difffields的数组。我试图在雪花上解析此列,而不是行阵列而不是行。 我尝试了变平,但这将其弄平为行。

我试图在DBT中解析此此表,以创建上面的JSON,其表结构为

create table some_table (
field_one,
--if `name` is present in the above json I want that to be 2nd column
-- if `active` is present in the above json i want that to be 3nd column 
)
)

{
  "segmentId": "b204c220-ea8d-4cf4-b579-30eb59a1a2a4",
  "diffFields": [
    {
      "fieldName": "name",
      "valueBefore": null,
      "valueAfter": "new-segment-name"
    },
    {
      "fieldName": "active",
      "valueBefore": null,
      "valueAfter": true
    }
  ]
}

In the above json I have an array of diffFields . I am trying to parse this in snowflake get the array of columns instead of rows.
I tried flatten, but this flatten it as rows.

I am trying to parse this in dbt to create another table from the above json with table structure as

create table some_table (
field_one,
--if `name` is present in the above json I want that to be 2nd column
-- if `active` is present in the above json i want that to be 3nd column 
)
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泅渡 2025-01-30 04:53:31

我会像以下情况下给它那样的弄平

WITH data as (
    select parse_json('
{
  "segmentId": "b204c220-ea8d-4cf4-b579-30eb59a1a2a4",
  "diffFields": [
    {
      "fieldName": "name",
      "valueBefore": null,
      "valueAfter": "new-segment-name"
    },
    {
      "fieldName": "active",
      "valueBefore": null,
      "valueAfter": true
    }
  ]
}') as json
)
select 
    json:segmentId::text as seg_id,
    f.value:fieldName::text as fieldName,
    f.value:valueBefore as valueBefore,
    f.value:valueAfter as valueAfter 
from data, table(flatten(input=>json:diffFields)) f

seg_id

fieldnamevalue之前
b204c220-e8d-4cf4-b579-30eb59a1a2a4名称num numnullnew-segment-name“
b204c220-ea8d-ea8d-ea8d-ea8d-4cf4-b579-309-30eb59a1a2anul

)空。因此,您想使用 is_null_value 测试和covert 要

选择数组零件:

select json:segmentId::text
    ,max(iff(f.value:fieldName::text = 'name', f.value, null)) as name_object
    ,max(iff(f.value:fieldName::text = 'active', f.value, null)) as active_object
from data, table(flatten(input=>json:diffFields)) f
group by 1;

json

:segmentId :: textname_objectactive_object
b204c220-ea8d-4cf4-b579-30eb59a1a2a4{“ fieldname”:“ name”,“ name”,“ new-segnmegname”,“ new-seak-name”,“ new-name” name“”, “:null}{“ fieldName”:“ active”,“ value ted”:true,dureateborfore':null}

I would flatten it like

WITH data as (
    select parse_json('
{
  "segmentId": "b204c220-ea8d-4cf4-b579-30eb59a1a2a4",
  "diffFields": [
    {
      "fieldName": "name",
      "valueBefore": null,
      "valueAfter": "new-segment-name"
    },
    {
      "fieldName": "active",
      "valueBefore": null,
      "valueAfter": true
    }
  ]
}') as json
)
select 
    json:segmentId::text as seg_id,
    f.value:fieldName::text as fieldName,
    f.value:valueBefore as valueBefore,
    f.value:valueAfter as valueAfter 
from data, table(flatten(input=>json:diffFields)) f

which gives:

SEG_IDFIELDNAMEVALUEBEFOREVALUEAFTER
b204c220-ea8d-4cf4-b579-30eb59a1a2a4namenull"new-segment-name"
b204c220-ea8d-4cf4-b579-30eb59a1a2a4activenulltrue

but those variant data nulls are not real nulls. so you want to use something like is_null_value to test and covert to real nulls

To select array parts:

select json:segmentId::text
    ,max(iff(f.value:fieldName::text = 'name', f.value, null)) as name_object
    ,max(iff(f.value:fieldName::text = 'active', f.value, null)) as active_object
from data, table(flatten(input=>json:diffFields)) f
group by 1;

gives:

JSON:SEGMENTID::TEXTNAME_OBJECTACTIVE_OBJECT
b204c220-ea8d-4cf4-b579-30eb59a1a2a4{ "fieldName": "name", "valueAfter": "new-segment-name", "valueBefore": null }{ "fieldName": "active", "valueAfter": true, "valueBefore": null }
筱果果 2025-01-30 04:53:31

您可以使用QuickTable连接雪花并进行JSON PARSE使用QuickTable。 QuickTable可以生成与雪花兼容的相关SQL。

You can use QuickTable to connect snowflake and do JSON parse use QuickTable. QuickTable can generate related SQL that is compatible with Snowflake.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文