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

如何在Jolt中添加Null值校验?场景配置问题求助

解决Jolt映射中null值的默认值设置问题

问题分析

你当前的Spec里,shift操作中的"null"是用来匹配键名为null的节点,而非值为null的phone字段,所以场景2的null校验逻辑无法生效。要实现phone为null时给customer.mobile设置默认值000的需求,需要结合shift和modify-default-beta操作完成。

正确的Jolt Spec

[
  {
    "operation": "shift",
    "spec": {
      "name": "name",
      "phone": {
        "*": {
          "mobile": "customer.mobile"
        }
      }
    }
  },
  {
    "operation": "modify-default-beta",
    "spec": {
      "customer": {
        "mobile": "000"
      }
    }
  }
]

场景验证

场景1:phone为数组输入

输入:

{
  "name": "Fred",
  "phone": [
    {
      "mobile": "1112223333"
    }
  ]
}

输出:

{
  "name" : "Fred",
  "customer" : {
    "mobile" : "1112223333"
  }
}

场景2:phone为null输入

输入:

{
  "name": "Fred",
  "phone": null
}

输出:

{
  "name" : "Fred",
  "customer" : {
    "mobile" : "000"
  }
}

逻辑说明

  1. shift操作:负责将正常phone数组中的mobile值映射到customer.mobile,同时保留name字段;当phone为null时,这一步不会生成customer.mobile节点。
  2. modify-default-beta操作:检查customer.mobile是否存在,若不存在(对应phone为null的场景),则将其设置为默认值000。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 08:31:05