如何使用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
相关产品推荐
相关产品推荐

