简体   繁体   English

解析 Snowflake 中嵌套的 JSON 字段

[英]Parsing nested JSON fields in Snowflake

I am pretty new to Snowflake and I am now trying to parse a JSON field and pull its attributes to return in the response.我是 Snowflake 的新手,我现在正在尝试解析 JSON 字段并提取其属性以在响应中返回。

I tried a few variations but every time, the attribute is populating as null .我尝试了一些变体,但每次都将属性填充为null

attributes column in my table has this JSON :我表中的attributes列有这个 JSON

{
    "Status": [
        "ACTIVE"
    ],
    "Coverence": [
        {
            "Sub": [
                {
                    "EndDate": [
                        "2020-06-22"
                    ],
                    "Source": [
                        "Test"
                    ],
                    "Id": [
                        "CovId1"
                    ],
                    "Type": [
                        "CovType1"
                    ],
                    "StartDate": [
                        "2019-06-22"
                    ],
                    "Status": [
                        "ACTIVE"
                    ]
                }
            ]
        }
    ]
}

What I tried :我试过的

SELECT DISTINCT *
    from
        (
            TRIM(mt."attributes":Status, '[""]')::string as STATUS,
            TRIM(r.value:"Sub"."Id", '[""]')::string as ID,
            TRIM(r.value:"Sub"."Source", '[""]')::string as SOURCE
            from "myTable" mt,
            lateral flatten ( input => mt."attributes":"Coverence", outer => true) r
        )
    GROUP BY
        STATUS,
        ID,
        SOURCE;

Later I tried :后来我试过

SELECT DISTINCT *
    from
        (
            TRIM(mt."attributes":Status, '[""]')::string as STATUS,
            TRIM(r.value:"Id", '[""]')::string as ID,
            TRIM(r.value:"Source", '[""]')::string as SOURCE
            from "myTable" mt,
            lateral flatten ( input => mt."attributes":"Coverence":"Sub", outer => true) r
        )
    GROUP BY
        STATUS,
        ID,
        SOURCE;

But nothing worked.但没有任何效果。 The STATUS is populating as expected. STATUS正在按预期填充。 But ID and SOURCE are populating null .但是IDSOURCE正在填充null

Am I missing something or have I done something dumb?我错过了什么或者我做了一些愚蠢的事情吗? Please shed some light.请说明一下。

Assuming that Coverence could contain multiple Sub , therefore FLATTEN twice.假设Coverence可以包含多个Sub ,因此 FLATTEN 两次。 At lowest level only first element is chosen ( EndDate[0] , Source[0] etc):在最低级别,仅选择第一个元素( EndDate[0]Source[0]等):

SELECT 
   mt."attributes":Status[0]::TEXT AS Status
  ,r2.value:EndDate[0]::TEXT AS EndDate
  ,r2.value:Source[0]::TEXT AS Source
  ,r2.value:Id[0]::TEXT AS Id
FROM myTable AS mt,
LATERAL FLATTEN(input => mt."attributes",
                path => 'Coverence',
                outer => true) r1,
LATERAL FLATTEN(input => r1.value,
                path => 'Sub',
                outer => true) r2; 

Output: Output:

在此处输入图像描述

All your elements are array type and the overall JSON is not making much sense... so to access all your individual elements, you have to use [] notation and then you can access the element values.你所有的元素都是数组类型,整体 JSON 没有多大意义......所以要访问你所有的单个元素,你必须使用 [] 表示法然后你可以访问元素值。 You don't need to use flatten also, if you just have to access individual elements via index.如果您只需要通过索引访问单个元素,则也不需要使用 flatten。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM