首页 > 解决方案 > 从雪花中的不良数据中清除 JSON 列

问题描述

我在 Snowflake 中有一个包含许多列的表,其中一列是名为 RECORD 的 JSON 列(检查附件图片),因此 JSON 列中有多个列,其中一些列包含错误数据,例如( // , * 或 \ \ ) 我想在雪花中创建函数来清理这个 JSON 列中的坏数据,我的 JSON 在下面

{
  "F000 //Start Date": "3/3/2016 21:04",
  "F001 End Date": "3/3/2016 23:20",
  "F002 *Response Type//": "IP Address",
  "F003 IP Address": "166.170.14.93",
  "F004 SurveyName\\": "6 Month",
} 

在此处输入图像描述

标签: jsonjson.netsnowflake-cloud-data-platform

解决方案


一种简单的方法可能是实现一个返回清理后的 JSON 的 Javascript UDF,如下所示。

CREATE OR REPLACE FUNCTION clean(record variant)
  RETURNS variant
  LANGUAGE JAVASCRIPT
  AS $$
  cleaned = {}
  for (const property in RECORD) {
    cleaned[property.replace(/\/\/|\\|\*/g, '')] = RECORD[property]
  }
  return cleaned
  $$;

with tbl as (select parse_json($1) record from values ('{
  "F000 //Start Date": "3/3/2016 21:04",
  "F001 End Date": "3/3/2016 23:20",
  "F002 *Response Type//": "IP Address",
  "F003 IP Address": "166.170.14.93",
  "F004 SurveyName\\\\": "6 Month",
}'))

select clean(record) from tbl;

{
  "F000 Start Date": "3/3/2016 21:04",
  "F001 End Date": "3/3/2016 23:20",
  "F002 Response Type": "IP Address",
  "F003 IP Address": "166.170.14.93",
  "F004 SurveyName": "6 Month"
}

推荐阅读