簡體   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. 有人可以幫我實現所需的 output 格式嗎?

樣品 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"
    }]}

我不知道如何用雪花分隔數組中的字符串對象。 通過使用以下查詢,我可以將整個字符串轉換為 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;

但我需要提取特定的字符串 object 值。 例如,將 1234 和 1239 等 id 值填充為 pipe 分隔,如下所示。

ISSUE_ID    ISSUE_KEY   SPRINT_ID
123456      SUE-1234    1234|1239

非常感謝任何關於此獲得預期結果的想法。 謝謝..

看起來您的 sprint [...]中的數據只是有關該 sprint 的詳細信息。 我認為實際上用每個 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

根據您的樣本數據,結果將如下所示。

橫向展平兩次以獲得 Sprint ID

請參閱下面的雪花參考文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM