从Json中提取键值--Mule Dataweave。

问题描述 投票:0回答:1

我无法从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" ) 的条件,我只需要得到'BaseNumber的总和'。

我相信通过使用 reduce 函数,但不知道如何在Mule4中做基于条件的 reduce. 不需要使用上述数据编织逻辑。我只需要得到特定的值,如果 InformationLineId =="1001" 其 "基数 "之和

如果问题不清楚,请告诉我。先谢谢你了。

dataweave mule-esb mule4
1个回答
1
投票

关于你的问题。 1)答案是 动态选择器 所以对于你的例子,你可以做 ts."${test}" 2)关于这个问题不清楚你想做什么,下次请把预期的输出结果贴出来,但我得到的是你想得到所有的 NumberBaseInformationLineId 是变量的值 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

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

© www.soinside.com 2019 - 2024. All rights reserved.