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

求助:修复Jolt spec以实现基于条件填充primaryEmail字段

修正Jolt Spec以提取主邮箱字段

需要编写Jolt Spec,根据emails数组中primary字段为true的条件,填充primaryEmail字段。以下是详细信息:

输入数据

[
  {
    "uid": "1234mark",
    "name": "mark",
    "userName": "markw",
    "displayName": "Mark W",
    "emails": [
      {
        "primary": false,
        "value": "mark@gmail.com"
      },
      {
        "primary": true,
        "value": "mark@hotmail.com"
      }
    ]
  },
  {
    "uid": "9876steve",
    "name": "steve",
    "userName": "stevew",
    "displayName": "Steve W",
    "emails": [
      {
        "primary": false,
        "value": "steve@gmail.com"
      },
      {
        "primary": true,
        "value": "steve@hotmail.com"
      }
    ]
  }
]

期望输出

[
  {
    "user": {
      "externalId": "1234mark",
      "name": "mark",
      "userName": "markw",
      "displayName": "Mark W",
      "primaryEmail": "mark@hotmail.com"
    }
  },
  {
    "user": {
      "externalId": "9876steve",
      "name": "steve",
      "userName": "stevew",
      "displayName": "Steve W",
      "primaryEmail": "steve@hotmail.com"
    }
  }
]

当前错误输出

[
  {
    "user": {
      "externalId": "1234mark",
      "name": "mark",
      "userName": "markw",
      "displayName": "Mark W"
    }
  },
  {
    "user": {
      "externalId": "9876steve",
      "name": "steve",
      "userName": "stevew",
      "displayName": "Steve W"
    }
  }
]

现有Jolt Spec

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "uid": "[&1].user.externalId",
        "name": "[&1].user.name",
        "userName": "[&1].user.userName",
        "displayName": "[&1].user.displayName",
        "title": "[&1].user.title",
        "emails": {
          "*": {
            "primary": {
              "true": {
                "@(2,value)": "primaryEmail"
              }
            }
          }
        }
      }
    }
  }
]

修正后的Jolt Spec

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "uid": "[&1].user.externalId",
        "name": "[&1].user.name",
        "userName": "[&1].user.userName",
        "displayName": "[&1].user.displayName",
        "title": "[&1].user.title",
        "emails": {
          "*": {
            "primary": {
              "true": {
                "@(2,value)": "[&4].user.primaryEmail"
              }
            }
          }
        }
      }
    }
  }
]

修正说明

原Spec的问题在于提取primaryEmail时未指定正确的目标路径,仅写了"primaryEmail",导致字段未被映射到对应的user对象下。修正时将目标路径改为"[&4].user.primaryEmail":

  • @(2,value) 正确引用了当前email节点的value字段(往上两级);
  • [&4] 定位到最外层数组的对应用户索引(从true节点往上四级,对应外层的用户项);
  • 最终将符合条件的邮箱值正确放入user对象的primaryEmail字段中。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 12:45:36