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

如何使用JSONata Transform运算符更新单个对象属性?

JSONata Transform运算符更新属性不生效问题解决

问题场景

输入对象:

{
  "item": {
    "_id": "097369183479824602",
    "name": "Name",
    "description": "Description",
    "sku": "0230115",
    "quantity": 8
  },
  "quantityRemaining": 6
}

使用Transform表达式尝试将item.quantity替换为根对象的quantityRemaining值:

$ ~> | item | { "quantity" : quantityRemaining } |

实际输出中item.quantity仍为8,未被替换为6;但使用常量值(如3)时,表达式可正常更新属性。

问题原因

在Transform运算符的| item |上下文里,当前作用域是item对象,直接写quantityRemaining会尝试在item内部查找该属性。由于item中不存在这个属性,所以不会对原quantity值做修改。

解决方法

需要显式引用根对象的quantityRemaining,可以用$parent(父级上下文)或$$(全局上下文):

方法1:使用$parent引用父级

$ ~> | item | { "quantity" : $parent.quantityRemaining } |

方法2:使用$$引用全局上下文

$ ~> | item | { "quantity" : $$.quantityRemaining } |

验证结果

使用上述任一表达式后,输出符合预期:

{
  "item": {
    "_id": "097369183479824602",
    "name": "Name",
    "description": "Description",
    "sku": "0230115",
    "quantity": 6
  },
  "quantityRemaining": 6
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 06:00:18