简体   繁体   English

Snowflake - 展平来自 json 变体列的多个嵌套数组值

[英]Snowflake - flatten multiple nested array values from json variant column

I have a JSON data from which I wanted to extract the values of key 'text' with delimited in single row.我有一个 JSON 数据,我想从中提取键“文本”的值,并在单行中分隔。 Any help to achieve the desired output is much appreciated.非常感谢任何帮助实现所需的 output 的帮助。

Sample JSON data:样本 JSON 数据:

{
"expand": "schema,names",
"issues": [
        {
            "id": "123456",
            "key": "XYZ-123",
            "fields": {
                "customfield_10000": "abcd",
                "customfield_10001": 7,
                "customfield_10002": null,
                "description": {
                            "version": 1,
                            "type": "doc",
                            "content": [
                                {
                                    "type": "paragraph",
                                    "content": [
                                        {
                                            "type": "text",
                                            "text": "some text value 1"
                                        }
                                    ]
                                },
                                {
                                    "type": "paragraph",
                                    "content": [
                                        {
                                            "type": "text",
                                            "text": "some text value 2"
                                        },
                                        {
                                            "type": "text",
                                            "text": "some text value 3",
                                            "marks": [
                                                {
                                                    "type": "link",
                                                    "attrs": {
                                                        "href": "some ref"
                                                    }
                                                }
                                            ]
                                        },
                                        {
                                            "type": "text",
                                            "text": "some text value 4"
                                        }
                                    ]
                                },
                                {
                                    "type": "blockquote",
                                    "content": [
                                        {
                                            "type": "paragraph",
                                            "content": [
                                                {
                                                    "type": "text",
                                                    "text": "some text value 5"
                                                }
                                            ]
                                        },
                                        {
                                            "type": "paragraph",
                                            "content": [
                                                {
                                                    "type": "inlineCard",
                                                    "attrs": {
                                                        "url": "some url"
                                                    }
                                                },
                                                {
                                                    "type": "text",
                                                    "text": "some text value 6"
                                                }
                                            ]
                                        }
                                    ]
                                },
                                {
                                    "type": "paragraph",
                                    "content": [
                                        {
                                            "type": "text",
                                            "text": "some text value 7"
                                        }
                                    ]
                                },
                                {
                                    "type": "paragraph",
                                    "content": [
                                        {
                                            "type": "text",
                                            "text": "some text value 8"
                                        },
                                        {
                                            "type": "text",
                                            "text": "some text value 9",
                                            "marks": [
                                                {
                                                    "type": "link",
                                                    "attrs": {
                                                        "href": "some link"
                                                    }
                                                }
                                            ]
                                        }
                                    ]
                                },
                                {
                                    "type": "paragraph",
                                    "content": [
                                        {
                                            "type": "text",
                                            "text": "some text value 10"
                                        }
                                    ]
                                },
                                {
                                    "type": "paragraph",
                                    "content": [
                                        {
                                            "type": "text",
                                            "text": "some text value 11"
                                        }
                                    ]
                                },
                                {
                                    "type": "paragraph",
                                    "content": [
                                        {
                                            "type": "text",
                                            "text": "some text value 12"
                                        }
                                    ]
                            }
                        ]
                    }
                }
            }
        ]
}

Desired output:所需的 output:

ISSUE_ID ISSUE_KEY CF_10000 CF_10001 CF_10002   DESCRIPTION
123456   XYZ-123   abcd     7        null       some text value 1|some text value 2|some text value 3.....

I'm using the below query to get the arrays values.我正在使用以下查询来获取 arrays 值。 However I wanted the key 'text' values from arrays to be populated as above desired format.但是,我希望将 arrays 中的键“文本”值填充为上述所需格式。

    select
       ISSUE.value:id::number as ISSUE_ID,
       ISSUE.value:key::varchar as ISSUE_KEY,
       ISSUE.value:fields.customfield_10000::varchar as CF_10000,
       ISSUE.value:fields.customfield_10001::number as CF_10001,
       ISSUE.value:fields.customfield_10002::varchar as CF_10002,
       ISSUE.value:fields.description.content::varchar as DESCRIPTION
    from
       VARIANT_TABLE,
       lateral flatten( input => payload_json:issues, outer => true) as ISSUE

I have an UDF created to extract JSON array object key value into string of array, but that doesn't helped me to get the desired output from above shared JSON as it has nested arrays inside objects.我创建了一个 UDF,用于将 JSON 数组 object 键值提取到数组的字符串中,但这并不能帮助我从上面共享的 JSON 中获得所需的 output,因为它在对象内部嵌套了 arrays。

create or replace function UDF_ARRAY_OBJECT_TO_STRING_ARRAY(a array, b varchar)
returns array
language javascript
strict
comment = 'UDF to extract JSON array object key value into string of array. A refers to input array and B refers to extract which key from the array object'
as $$
return A.map(function(d) {return d[B]});
$$;

You have a lot more arrays in there than you are handling in your lateral flattens.你那里的 arrays 比你在横向展平中处理的要多得多。 With a few more flattens and a listagg() function, you should get there with this.有了更多的展平和一个 listagg() function,你应该可以做到这一点。 Note, you might need to group by the index, rather than the field values, depending on what you are trying to get to, but this gives the result you were looking for in your example:请注意,您可能需要按索引而不是字段值进行分组,具体取决于您要访问的内容,但这会给出您在示例中寻找的结果:

WITH x AS (
SELECT parse_json('{
"expand": "schema,names",
"issues": [
        {
            "id": "123456",
            "key": "XYZ-123",
            "fields": {
                "customfield_10000": null,
                "customfield_10001": null,
                "customfield_10002": null,
                "description": {
                            "version": 1,
                            "type": "doc",
                            "content": [
                                {
                                    "type": "paragraph",
                                    "content": [
                                        {
                                            "type": "text",
                                            "text": "some text value 1"
                                        }
                                    ]
                                },
                                {
                                    "type": "paragraph",
                                    "content": [
                                        {
                                            "type": "text",
                                            "text": "some text value 2"
                                        },
                                        {
                                            "type": "text",
                                            "text": "some text value 3",
                                            "marks": [
                                                {
                                                    "type": "link",
                                                    "attrs": {
                                                        "href": "some ref"
                                                    }
                                                }
                                            ]
                                        },
                                        {
                                            "type": "text",
                                            "text": "some text value 4"
                                        }
                                    ]
                                },
                                {
                                    "type": "blockquote",
                                    "content": [
                                        {
                                            "type": "paragraph",
                                            "content": [
                                                {
                                                    "type": "text",
                                                    "text": "some text value 5"
                                                }
                                            ]
                                        },
                                        {
                                            "type": "paragraph",
                                            "content": [
                                                {
                                                    "type": "inlineCard",
                                                    "attrs": {
                                                        "url": "some url"
                                                    }
                                                },
                                                {
                                                    "type": "text",
                                                    "text": "some text value 6"
                                                }
                                            ]
                                        }
                                    ]
                                },
                                {
                                    "type": "paragraph",
                                    "content": [
                                        {
                                            "type": "text",
                                            "text": "some text value 7"
                                        }
                                    ]
                                },
                                {
                                    "type": "paragraph",
                                    "content": [
                                        {
                                            "type": "text",
                                            "text": "some text value 8"
                                        },
                                        {
                                            "type": "text",
                                            "text": "some text value 9",
                                            "marks": [
                                                {
                                                    "type": "link",
                                                    "attrs": {
                                                        "href": "some link"
                                                    }
                                                }
                                            ]
                                        }
                                    ]
                                },
                                {
                                    "type": "paragraph",
                                    "content": [
                                        {
                                            "type": "text",
                                            "text": "some text value 10"
                                        }
                                    ]
                                },
                                {
                                    "type": "paragraph",
                                    "content": [
                                        {
                                            "type": "text",
                                            "text": "some text value 11"
                                        }
                                    ]
                                },
                                {
                                    "type": "paragraph",
                                    "content": [
                                        {
                                            "type": "text",
                                            "text": "some text value 12"
                                        }
                                    ]
                            }
                        ]
                    }
                }
            }
        ]
}') as payload_json)
    select
       issue.value:id::number as ISSUE_ID,
       issue.value:key::varchar as ISSUE_KEY,
   ISSUE.value:fields.customfield_10000::varchar as CF_10000,
   ISSUE.value:fields.customfield_10001::number as CF_10001,
   ISSUE.value:fields.customfield_10002::varchar as CF_10002,
       listagg(content2.value:text::varchar,'|') as description
    from
       x,
       lateral flatten( input => x.payload_json:issues, outer => true) as issue,
       lateral flatten( input => issue.value:fields:description:content, outer => true) as content,
       lateral flatten( input => content.value:content, outer => true) as content2
    group by 1,2,3,4,5;

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

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