简体   繁体   English

雪花 - 将 JSON 数组字符串 object 值提取到 pipe 分隔值中

[英]Snowflake - extract JSON array string object values into pipe separated values

I have a nested JSON array which is a string object which has been stored into variant type stage table and I want to extract particular string object value and populate with pipe separated values if more than one object found. I have a nested JSON array which is a string object which has been stored into variant type stage table and I want to extract particular string object value and populate with pipe separated values if more than one object found. Can someone help me to achieve the desired output format please.有人可以帮我实现所需的 output 格式吗?

Sample JSON data样品 JSON 数据

    {"issues": [
    {
    "expand": "",
    "fields": {
        "customfield_10010": [
        "com.atlassian.xxx.yyy.yyyy.Sprint@xyz456[completeDate=2020-07-20T20:19:06.163Z,endDate=2020-07-17T21:48:00.000Z,goal=,id=1234,name=SPR-SPR 8,rapidViewId=239,sequence=1234,startDate=2020-06-27T21:48:00.000Z,state=CLOSED]",
        "com.atlassian.xxx.yyy.yyyy.Sprint@abc123[completeDate=<null>,endDate=2020-08-07T20:33:00.000Z,goal=,id=1239,name=SPR-SPR 9,rapidViewId=239,sequence=1239,startDate=2020-07-20T20:33:26.364Z,state=ACTIVE]"
        ],
        "customfield_10011": "obcd",
        "customfield_10024": null,
        "customfield_10034": null,
        "customfield_10035": null,
        "customfield_10037": null,
    },
    "id": "123456",
    "key": "SUE-1234",
    "self": "xyz"
    }]}

I don't have any idea on how to separate the string objects inside an array with snowflake.我不知道如何用雪花分隔数组中的字符串对象。 By using the below query I can get whole string converted into pipe separated values.通过使用以下查询,我可以将整个字符串转换为 pipe 分隔值。

select
    a.value:id::number as ISSUE_ID,
    a.value:key::varchar as ISSUE_KEY,
    array_to_string(a.value:fields.customfield_10010, '|') as CF_10010_Data
from
    ABC.VARIANT_TABLE,
    lateral flatten( input => payload_json:issues) as a;

But I need to extract particular string object value.但我需要提取特定的字符串 object 值。 Say for example id value such as 1234 & 1239 to be populated as pipe separated as shown below.例如,将 1234 和 1239 等 id 值填充为 pipe 分隔,如下所示。

ISSUE_ID    ISSUE_KEY   SPRINT_ID
123456      SUE-1234    1234|1239

Any idea on this to get desired result is much appreciated.非常感谢任何关于此获得预期结果的想法。 Thanks..谢谢..

It looks like the data within [...] for your sprints are just details about that sprint.看起来您的 sprint [...]中的数据只是有关该 sprint 的详细信息。 I think it would be easiest for you to actually populate a separate sprints table with data on each sprint, and then you can join that table to the Sprint ID values parsed from the API response you showed with issues data.我认为实际上用每个 sprint 的数据填充一个单独sprints表对您来说是最简单的,然后您可以将该表连接到从您显示的带有issues数据的 API 响应中解析的 Sprint ID 值。

with
    jira_responses as (
        select
            $1 as id,
            $2 as body
        from (values
            (1, '{"issues":[{"expand":"","fields":{"customfield_10010":["com.atlassian.xxx.yyy.yyyy.Sprint@xyz456[completeDate=2020-07-20T20:19:06.163Z,endDate=2020-07-17T21:48:00.000Z,goal=,id=1234,name=SPR-SPR 8,rapidViewId=239,sequence=1234,startDate=2020-06-27T21:48:00.000Z,state=CLOSED]","com.atlassian.xxx.yyy.yyyy.Sprint@abc123[completeDate=<null>,endDate=2020-08-07T20:33:00.000Z,goal=,id=1239,name=SPR-SPR 9,rapidViewId=239,sequence=1239,startDate=2020-07-20T20:33:26.364Z,state=ACTIVE]"],"customfield_10011":"obcd","customfield_10024":null,"customfield_10034":null,"customfield_10035":null,"customfield_10037":null},"id":"123456","key":"SUE-1234","self":"xyz"}]}')
        )
    )
select
    issues.value:id::integer as issue_id,
    issues.value:key::string as issue_key,
    get(split(sprints.value::string, '['), 0)::string as sprint_id
from jira_responses,
lateral flatten(input => parse_json(body):issues) issues,
lateral flatten(input => parse_json(issues.value):fields:customfield_10010) sprints

Based on your sample data, the results would look like the following.根据您的样本数据,结果将如下所示。

横向展平两次以获得 Sprint ID

See Snowflake reference docs below.请参阅下面的雪花参考文档。

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

相关问题 Php - 如何将多个键值数组转换为| (管道)分隔的字符串 - Php - How to convert multiple key values array to | (Pipe) Separated string 如何分割由管道分隔的值的字符串并返回数组? - How do you split a string of pipe separated values and return an array? 如何将管道分隔的字符串拆分为对象数组 - How to split pipe separated string into array of object 如何从一串逗号分隔值创建一个 json 数组字符串? - How to create a json array string from a string of comma separated values? 具有键号和重复字符串值的 JSON 对象分隔为带有计数的字符串数组 - JSON object with key numbers and values of recurring strings separated in to string array with count 从字典中提取数组值<string, object></string,> - Extract array values from Dictionary<string, object> 将 json 中的逗号分隔字符串或 int 值解组到数组 - Unmarshal comma-separated string or int values in json to an array 将对象值提取到数组中 - Extract Object Values into an Array 在逗号分隔的字符串中如何过滤数组 object 的唯一值 - How to filter unique values of array object when in comma separated string 解析在 Snowflake 中的值之一中具有数组的 JSON - Parsing a JSON that has an array in one of the values in Snowflake
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM