我需要得到数据编织的订单的总价。
<?xml version='1.0' encoding='UTF-8'?>
<Orders>
<Order id="10001">
<ProductId id="P001">P-001</ProductId>
<ProductName>Samsung 40Inch TV</ProductName>
<Category id="C001">Samsung TV</Category>
<Price>399</Price>
<Quantity>5</Quantity>
</Order>
<Order id="10001">
<ProductId id="P002">P-002</ProductId>
<ProductName>Samsung 32Inch TV</ProductName>
<Category id="C001">Samsung TV</Category>
<Price>299</Price>
<Quantity>5</Quantity>
</Order>
</Orders>我尝试过以下数据编织,但没有成功:
%dw 2.0
output application/json
---
{
"totalCost": sum(payload.Orders.*Order.Price)
}发布于 2022-04-22 06:37:53
以下代码将生成每个订单的总价。
%dw 2.0
output application/json
---
payload.Orders.*Order map(
"totalCost": $.Price * $.Quantity
)输出:-
[
{
"totalCost": 1995
},
{
"totalCost": 1495
}
]如果您想要所有订单的总成本,请使用下面的脚本
%dw 2.0
output application/json
---
"totalCost" : sum(payload.Orders.*Order map(
$.Price * $.Quantity
))输出:-
{
"totalCost": 3490
}发布于 2022-03-27 02:25:22
剧本似乎在按订单把所有的价格加在一起。由于问题不明确,我猜你需要计算订单的“总成本”,假设它的价格乘以数量。为此,您必须先将每一批订单映射到其价格*数量,然后再计算金额:
%dw 2.0
output application/json
---
{
"totalCost": sum(payload.Orders.*Order map ($.Price * $.Quantity))
}输出:
{
"totalCost": 3490
}https://stackoverflow.com/questions/71627533
复制相似问题