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

如何使用JSONata聚合数组:求和Price并提取首个ProductID

JSONata数据处理方案

原始数据

{ 
  "123": [ 
    { "Price": 34.45, "Qty": 2, "ProductID": 858383 }, 
    { "Price": 34.45, "Qty": 2, "ProductID": 85838388888 } 
  ], 
  "Trilby hat": { "Price": 21.67, "Qty": 1, "ProductID": 858236 } 
}

实现表达式

[
  $merge([
    { "Price": $sum(`123`.Price) },
    { "Qty": `123`[0].Qty },
    { "ProductID": `123`[0].ProductID }
  ]),
  `Trilby hat`
]

逻辑说明

  • 针对键123对应的数组:
    • 用$sum(123.Price)计算所有元素的Price总和;
    • 通过索引[0]提取数组首个元素的Qty和ProductID;
    • 用$merge()将单独构造的字段对象合并为一个完整对象;
  • 将处理后的对象与Trilby hat对应的原始对象共同放入数组,得到目标结构。

最终结果

[ 
  { "Price": 68.9, "Qty": 2, "ProductID": 858383 }, 
  { "Price": 21.67, "Qty": 1, "ProductID": 858236 } 
]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 00:42:38