You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用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), '\\}+$', ']')"
      }
    }
  }
]

说明

  1. modify-overwrite-beta操作可对指定字段执行字符串替换修改,同时保留其他字段原有结构。
  2. 两次replaceAll分别处理首尾符号:
    • ^\\{+匹配字符串开头的一个或多个连续{,替换为[
    • \\}+$匹配字符串结尾的一个或多个连续},替换为]
  3. 规则兼容filters字段值首尾有多个连续大括号的场景,仅替换最外层符号,不影响字段内部结构。

内容的提问来源于stack exchange,提问作者Vlad Aleshin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 14:12:52