在Snowflake中解析JSON

问题描述 投票:0回答:2

我正在尝试使用Snowflake中的横向函数在Snowflake中解析以下嵌套的JSON,但是我希望“ GoalTime”中的每个嵌套列都显示为一列。例如,>

GoalTime_InDoorOpen         
2020-03-26T12:58:00-04:00   

GoalTime_InLastOff
null

GoalTime_OutStartBoarding
2020-03-27T14:00:00-04:00  
"GoalTime": [
    {
      "GoalName": "GoalTime_InDoorOpen",
      "GoalTime": "2020-03-26T12:58:00-04:00"
    },
    {
      "GoalName": "GoalTime_InLastOff"
    },
    {
      "GoalName": "GoalTime_InReadyToTow"
    },
    {
      "GoalName": "GoalTime_OutTowAtGate"
    },
    {
      "GoalName": "GoalTime_OutStartBoarding",
      "GoalTime": "2020-03-27T14:00:00-04:00"
    },

我正在尝试使用Snowflake中的横向函数在Snowflake中解析以下嵌套的JSON,但是我希望“ GoalTime”中的每个嵌套列都显示为一列。例如,...

sql json azure snowflake-cloud-data-platform
2个回答
1
投票

或者如果您有很多行(看起来像是排期),因此您需要为每个排期添加此代码列,就是您想要的代码


0
投票
create or replace function JSON_STRING()
returns string
language javascript
as
$$
return `
[
  {
        "GoalName": "GoalTime_InDoorOpen",
        "GoalTime": "2020-03-26T12:58:00-04:00"
    },
    {
        "GoalName": "GoalTime_InLastOff"
    },
    {
        "GoalName": "GoalTime_InReadyToTow"
    },
    {
        "GoalName": "GoalTime_OutTowAtGate"
    },
    {
        "GoalName": "GoalTime_OutStartBoarding",
        "GoalTime": "2020-03-27T14:00:00-04:00"
    }
]
`;
$$;

select value:GoalName::string as GoalName, value:GoalTime::timestamp as GoalTime
from lateral flatten(input => parse_json(JSON_STRING()));

-- See how the lateral flatten combination works on a JSON variant:
select * from lateral flatten(input => parse_json(JSON_STRING()));
© www.soinside.com 2019 - 2024. All rights reserved.