如何从snowflake中的JSON变体中提取值?

6l7fqoea  于 2023-06-25  发布在  其他
关注(0)|答案(1)|浏览(137)

我将数据从S3复制到snowflake作为snowflake stage中的JSON变量,但不确定如何从字符串中提取值并加载到另一个表。
采取的步骤:
/* created json format */create or replace file format jsonformat type ='JSON 'strip_outer_array = true;
/创建了存储JSON变量的表/CREATE OR REPLACE TABLE xyz. abc. CAN_IB_SCHEDULING_test(JSON_DATA VARIANT);
/将JSON数据从snowflake阶段复制到上面创建的表/COPY INTO CAN_IB_SCHEDULING_test FROM@xyz. abc. CAN_IB_SCHEDULING_STG FILE_FORMAT =(format_name = jsonformat);
从CAN_IB_SCHEDULING_test中选择JSON_DATA;
JSON变体示例
{"预约日期":"{"columnId":8275473196863364,"value":"2023 - 06 - 07","objectValue":{" objectType ":" DATE "," value ":" 2023 - 06 - 07 "},"format":",,,,,,,,"}","约会ID":"{"columnId":8331000681549700,"value":"MSKW000001","objectValue":"MSKW000001","displayValue":"MSKW000001","format":",,,,,,,"}","预约插入日期":"{"columnId":3771873569492868,"value":"2023 - 06 - 02T18:08:56Z","objectValue":{" objectType ":" DATETIME "," value ":" 2023 - 06 - 02T18:08:56Z "},"format":",,,,,,,,,,"}",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",","{"columnId":2699982876397444,"value":"请求的运营商","objectValue":"请求的运营商","displayValue":"请求的运营商","format":",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
当我为特定列选择值时,它返回null。

  • SELECT JSON_DATA:"约会日期". value FROM JSON_table;*

预期输出:

    • 预约日期预约ID预约备注2023 - 06 - 07 DFSW00001承运人请求**
vm0i2vca

vm0i2vca1#

因此,为了从Snowflake中的JSON变量中提取值,可以使用Snowflake的JSON解析函数,如下所示:*sql

SELECT
      JSON_DATA:"Appointment Date":value::string AS "Appointment Date",
      JSON_DATA:"Appointment ID":value::string AS "Appointment ID",
      JSON_DATA:"Appointment Remark":value::string AS "Appointment Remark"
    FROM CAN_IB_SCHEDULING_test;

相关问题