从 Json 中提取键值 - Mule Dataweave

Extracting key Value from Json - Mule Dataweave

我无法从 Json 获取键值。请在下面找到 xml 到 Json 转换的详细信息。

输入XML:

 <DeliveryDetails>
  <Information>
    <InformationLines>
        <InformationLine>
            <InformationLineId>1001</InformationLineId>
            <ProductId>PREBC518</ProductId>
            <ProductDescr>Bag  </ProductDescr>
            <NumberBase>150.00000000</NumberBase>
            <UnitBase>BAG</UnitBase>               
        </InformationLine>
        <InformationLine>
            <InformationLineId>1001</InformationLineId>
            <ProductId>PREBC518</ProductId>
            <ProductDescr>Bag delivery</ProductDescr>
            <NumberBase>150.00</NumberBase>
            <UnitBase>BAG</UnitBase>               
        </InformationLine>
        <InformationLine>
            <InformationLineId>1003</InformationLineId>
            <ProductId>PREBC518</ProductId>
            <ProductDescr>test</ProductDescr>
            <NumberBase>70.00</NumberBase>
            <UnitBase>BAG</UnitBase>               
        </InformationLine>
        <InformationLine>
            <InformationLineId>1005</InformationLineId>
            <ProductId>PREBC518</ProductId>
            <ProductDescr> dress </ProductDescr>
            <NumberBase>80.00</NumberBase>
            <UnitBase>BAG</UnitBase>               
        </InformationLine>               
    </InformationLines>
</Information>

       %dw 2.0
       output application/json
       var nb =  payload.DeliveryDetails.Information.InformationLines.*InformationLine groupBy 
          ($.InformationLineId  ) mapObject (value,key) -> { 
          (key): sum (value.NumberBase)
           }
       var test = "1001"
       ---
       nb

以上表达式给出以下结果

        {

          "1005": "80.00",
          "1003": "70.00",
          "1001": 300.00
        }

有 2 个问题

1) 当我直接使用 ts.'1001' 我可以得到 "300.00" 如果我使用 "ts.test" 它没有获取值?,理想情况下在上面 test 可以分配给不同的值,因此我正在寻找动态检索。

2) 使用上面的 XML 基于条件的输入 "($.InformationLineId == "1001" ) 我只需要得到 'sum of BaseNumber'。

我相信它可以通过在 Mule4 中使用 reduce 函数轻松实现,但不确定如何根据 reduce 执行条件。不需要使用上述数据编织逻辑。如果 InformationLineId =="1001" 其总和 "BaseNumber"

如果问题不清楚,请告诉我。提前致谢。

关于您的问题: 1) 答案是 dynamic selector 所以对于你的例子你可以做 ts."${test}" 2)关于这个问题不清楚你下次想做什么请 post 预期输出但我得到的是你试图获得所有 NumberBase 的总和 InformationLineId 是变量 test 的值。如果是这样,那么答案将是

%dw 2.0
output application/json

var test = "1001"

var nb =  sum(payload.DeliveryDetails.Information.InformationLines.*InformationLine 
            filter ($.InformationLineId == test) 
            map ((value) -> value.NumberBase as Number))

---
nb

如果不是,请重新表述您的问题