json - 从雪花中的不良数据中清除 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",
}
解决方案
一种简单的方法可能是实现一个返回清理后的 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"
}
推荐阅读
- c# - 使用 Socket 从机器接收数据
- python-3.x - 是否可以在脚本中安装 pip 包并在同一脚本中使用该包?
- visual-studio-code - 减慢 vscode python linting 中的问题检测
- visual-studio - 如何根据操作系统从构建中排除 .NET 5.0 项目
- r - 在 R 中使用 c() 或 as.vector() 代替警告
- c++ - 如何在 C++ 猜谜游戏中包含一个范围并让代码重复?
- android - Android 12 - Google Pixel 无法从 BubbleActivity 启动 MainActivity
- spring-boot - 如何在多模块spring boot应用程序gradle中只访问不同模块的一个类?
- excel - Excel:单元格范围,列中的第一个单元格到同一列中的倒数第二个单元格
- laravel - Pusher Laravel 广播离开房间通知