Star
Star

Reputation: 1503

Extracting key Value from Json - Mule Dataweave

I'm unable to get the key value from Json. Please find the details below xml to Json conversion.

Input 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

Above expression gives the below result

        {

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

Having 2 questions

1) when i directly use ts.'1001' i can able to get "300.00" if i use "ts.test" it is not fetching the value?, ideally in the above test can be assigned to different value hence im looking for dynamic retrieval.

2) Using the above XML Input on condition based "($.InformationLineId == "1001" ) i only need to get 'sum of BaseNumber'.

I believe it easily achieve by using reduce function in Mule4 but not sure how to do condition based in reduce. Needn't required to use the above dataweave logic. I just need to get specific value if InformationLineId =="1001" its sum of "BaseNumber"

Please let me know if the question is not clear. Thanks in advance.

Upvotes: 1

Views: 695

Answers (1)

machaval
machaval

Reputation: 5059

Regarding your questions: 1) the answer is dynamic selector so for your example you can do ts."${test}" 2) Regarding this question is not clear what you are trying to do next time please post the expected output but what I get is that you are trying to get the sum of all NumberBase whom InformationLineId is the value of variable test. If so then the answer will be

%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

If not please rephrase your question

Upvotes: 1

Related Questions