如何使用JOLT转换将filters字段的外层大括号替换为方括号
使用JOLT替换JSON指定字段的外层符号
需求:将JSON数据中after对象下filters字段值的外层大括号(无论开头有多少个连续{、结尾有多少个连续})替换为方括号,其余字段保持不变。
输入JSON示例
{ "after": { "import_id": 912, "date": "2021-01-21 00:00:00.0", "filters": "{{{NeverCheckSupplier: {supplier: 5}}, {NeverCheckSupplier: {supplier: 5}}}", "entries": "{1952253,1952252,1952251}", "commit_time": "2021-01-21 13:20:48.630796", "id": 1 }, "table": "commits", "op_type": "c" }
期望输出JSON示例
{ "after": { "import_id": 912, "date": "2021-01-21 00:00:00.0", "filters": "[{NeverCheckSupplier: {supplier: 5}}, {NeverCheckSupplier: {supplier: 5}}]", "entries": "{1952253,1952252,1952251}", "commit_time": "2021-01-21 13:20:48.630796", "id": 1 }, "table": "commits", "op_type": "c" }
JOLT转换规范
[ { "operation": "modify-overwrite-beta", "spec": { "after": { // 替换开头所有连续的{为[ "filters": "=replaceAll(@(1), '^\\{+', '[')", // 替换结尾所有连续的}为] "filters": "=replaceAll(@(1), '\\}+$', ']')" } } } ]
说明
modify-overwrite-beta操作可对指定字段执行字符串替换修改,同时保留其他字段原有结构。- 两次
replaceAll分别处理首尾符号:^\\{+匹配字符串开头的一个或多个连续{,替换为[\\}+$匹配字符串结尾的一个或多个连续},替换为]
- 规则兼容
filters字段值首尾有多个连续大括号的场景,仅替换最外层符号,不影响字段内部结构。
内容的提问来源于stack exchange,提问作者Vlad Aleshin
相关产品推荐
相关产品推荐

