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

编写满足双条件过滤的JOLT Spec遇问题求助

JOLT转换需求:修改满足双条件的对象value值

输入JSON

{
  "value": [
    {
      "identifier": "plm",
      "value": "3",
      "value_unit_key": "",
      "language": ""
    },
    {
      "identifier": "mytest",
      "value": "whatever",
      "value_unit_key": "",
      "language": ""
    },
    {
      "identifier": "new ",
      "value": "anything",
      "value_unit_key": "kg",
      "language": ""
    }
  ]
}

期望输出JSON

仅将identifier为plm且value为3的对象的value改为active,其余所有字段保持不变:

{
  "value": [
    {
      "identifier": "plm",
      "value": "active",
      "value_unit_key": "",
      "language": ""
    },
    {
      "identifier": "mytest",
      "value": "whatever",
      "value_unit_key": "",
      "language": ""
    },
    {
      "identifier": "new ",
      "value": "anything",
      "value_unit_key": "kg",
      "language": ""
    }
  ]
}

尝试的JOLT Spec及问题

以下是尝试的Spec:

[
  {
    "operation": "shift",
    "spec": {
      "value": {
        "*": {
          "value_unit_key": "value[#2].value_unit_key",
          "language": "value[#2].language",
          "identifier": {
            "plm": {
              "@(2,value)": {
                "3": {
                  "#active": "value[#1].value",
                  "#plm": "value[#1].identifier"
                }
              }
            },
            "*": {
              "identifier": "value[#2].identifier",
              "value": "value[#2].value"
            }
          }
        }
      }
    }
  }
]

当前输出缺少非目标对象的identifier和value字段:

{
  "value": [
    {
      "identifier": "plm",
      "language": "",
      "value": "active",
      "value_unit_key": ""
    },
    {
      "language": "",
      "value_unit_key": ""
    },
    {
      "language": "",
      "value_unit_key": "kg"
    }
  ]
}

正确的JOLT Spec

方案一:两步操作(直观易维护)

先完整复制所有数据,再针对符合条件的对象修改value:

[
  // 第一步:复制所有原始数据到输出
  {
    "operation": "shift",
    "spec": {
      "*": "&" // 复制顶层所有字段(此处为value数组)
    }
  },
  // 第二步:修改满足条件的对象的value值
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "value": {
        "*": {
          // 条件判断:identifier为plm且value为3时,替换为active,否则保留原值
          "value": "=cond(@(1,identifier) == 'plm' && @ == '3', 'active', @)"
        }
      }
    }
  }
]

方案二:单步shift操作

通过先复制所有字段,再覆盖目标字段的方式实现:

[
  {
    "operation": "shift",
    "spec": {
      "value": {
        "*": {
          // 先复制当前对象的所有字段到对应数组位置
          "*": "value[#2].&",
          // 针对符合条件的情况,覆盖value字段
          "@(1,identifier)": {
            "plm": {
              "@(2,value)": {
                "3": {
                  "#active": "value[#4].value"
                }
              }
            }
          }
        }
      }
    }
  }
]

注:shift操作中后匹配的规则会覆盖先匹配的,因此能实现目标字段的替换。

内容的提问来源于stack exchange,提问作者Katja Bürger

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 10:17:47