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

JOLT转换问题:如何将JSON中currentCondition字段设为固定值Snow并移除geoLocation字段

我来帮你搞定这个JOLT脚本的问题!你的脚本之所以会丢失currentCondition字段,是因为你对这个字段的Shift操作写法有误,没匹配到原数据里的字符串值。

问题原因分析

原脚本里的"currentCondition": { "*": { "Snow": "currentCondition" } }是在尝试匹配currentCondition的子字段,但你的currentCondition是一个直接的字符串值(不是对象结构),所以这个路径根本找不到匹配项,自然就没把这个字段带到输出里。

修正方案

这里提供两种可行的修正方式,都能实现你要的效果:

方案一:单Shift操作完成所有需求

直接在Shift操作里,一边保留其他字段,一边给currentCondition硬编码赋值:

[
  {
    "operation": "shift",
    "spec": {
      "geoLocation": null, // 移除geoLocation字段
      "*": "&", // 保留除geoLocation外的所有原有字段
      "#Snow": "currentCondition" // 直接将currentCondition设为固定值Snow
    }
  }
]

这里的#符号在JOLT里表示硬编码字符串值,不管原currentCondition是什么,都会被这个固定值覆盖。

方案二:分两步操作(逻辑更清晰)

先通过Shift移除geoLocation,再用modify-overwrite-beta操作强制设置currentCondition的值:

[
  {
    "operation": "shift",
    "spec": {
      "geoLocation": null,
      "*": "&"
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "currentCondition": "Snow"
    }
  }
]

这种方式把字段移除和值覆盖拆成两步,逻辑更直观,也适合后续扩展更复杂的转换规则。

验证效果

两种方案执行后,都会得到你预期的输出:

{
  "locationId": 100212,
  "weatherDateGmt": 1642590000,
  "humidity": 77,
  "currentCondition": "Snow",
  "latitude": 43.80306,
  "longitude": 143.89083
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 10:37:27