0
votes

I am getting the below xml response from a Web service, what i want to achieve is as below: 1. Find the largest SEQ_NO from the response. 2. Format the response as shown below in the expected one but the dataweave (one i pasted below) i have written is not getting the desired output.

Could you please help !!

Sample Payload

<?xml version="1.0" encoding="UTF-8"?>
<GSBResponse xmlns="http://sampleorg.org/" xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
    <GSBResult xmlns:b="http://schemas.org/2004/07/DummyServ" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <b:Reply>0</b:Reply>
        <b:UID>3001</b:UID>
        <b:Counts>
            <b:GBCContent>
                <b:SEQ_NO>1</b:SEQ_NO>
                <b:BNO>112233-444</b:BNO>
                <b:BTYPE>A</b:BTYPE>
                <b:DC>2018-10-08T16:31:36.0754167+02:00</b:DC>
                <b:Values>
                    <b:GBCV>
                        <b:Code>000379</b:Code>
                        <b:Count>2</b:Count>
                        <b:Amount>100</b:Amount>
                    </b:GBCV>
                </b:Values>
                <b:TNO>8934</b:TNO>
                <b:RNO>3334</b:RNO>
            </b:GBCContent>
            <b:GBCContent>
                <b:SEQ_NO>8</b:SEQ_NO>
                <b:BNO>112233-555</b:BNO>
                <b:BTYPE>S</b:BTYPE>
                <b:DC>2018-10-08T16:31:36.0754167+02:00</b:DC>
                <b:Values>
                    <b:GBCV>
                        <b:Code>000379</b:Code>
                        <b:Count>2</b:Count>
                        <b:Amount>100</b:Amount>
                    </b:GBCV>
                    <b:GBCV>
                        <b:Code>000400</b:Code>
                        <b:Count>3</b:Count>
                        <b:Amount>200</b:Amount>
                    </b:GBCV>
                </b:Values>
                <b:TNO>8934</b:TNO>
                <b:RNO>3334</b:RNO>
            </b:GBCContent>
        </b:Counts>
    </GSBResult>
</GSBResponse>

Output I'm getting:

[
  {
    "SEQ_NO": "1",
    "STORE_NUMBER": "3001",
    "DC": "2018-10-08T16:31:36.0754167+02:00",
    "BNO": "112233-444",
    "BTYPE": "A",
    "COUNT_NUMBER": "8934",
    "REGISTRATION_NUMBER": "3334",
    "Code": "000379",
    "Count": "2",
    "Amount": "100"
  },
  {
    "SEQ_NO": "8",
    "STORE_NUMBER": "3001",
    "DC": "2018-10-08T16:31:36.0754167+02:00",
    "BNO": "112233-555",
    "BTYPE": "S",
    "COUNT_NUMBER": "8934",
    "REGISTRATION_NUMBER": "3334",
    "Code": "000379",
    "Count": "2",
    "Amount": "100",
    "SEQ_NO": "8",
    "STORE_NUMBER": "3001",
    "DC": "2018-10-08T16:31:36.0754167+02:00",
    "BNO": "112233-555",
    "BTYPE": "S",
    "COUNT_NUMBER": "8934",
    "REGISTRATION_NUMBER": "3334",
    "Code": "000400",
    "Count": "3",
    "Amount": "200"
  }
]

Expected Output

[
  {
    "SEQ_NO": "1",
    "STORE_NUMBER": "3001",
    "DC": "2018-10-08T16:31:36.0754167+02:00",
    "BNO": "112233-444",
    "BTYPE": "A",
    "COUNT_NUMBER": "8934",
    "REGISTRATION_NUMBER": "3334",
    "Code": "000379",
    "Count": "2",
    "Amount": "100"
  },
  {
    "SEQ_NO": "8",
    "STORE_NUMBER": "3001",
    "DC": "2018-10-08T16:31:36.0754167+02:00",
    "BNO": "112233-555",
    "BTYPE": "S",
    "COUNT_NUMBER": "8934",
    "REGISTRATION_NUMBER": "3334",
    "Code": "000379",
    "Count": "2",
    "Amount": "100",
               },
               {
    "SEQ_NO": "8",
    "STORE_NUMBER": "3001",
    "DC": "2018-10-08T16:31:36.0754167+02:00",
    "BNO": "112233-555",
    "BTYPE": "S",
    "COUNT_NUMBER": "8934",
    "REGISTRATION_NUMBER": "3334",
    "Code": "000400",
    "Count": "3",
    "Amount": "200"
  }
]

Data Weave

        %dw 1.0
        %output application/java
        %namespace ns0 http://schemas.org/2004/07/DummyServ
        %namespace ns01 http://sampleorg.org/
        ---
        (payload.ns01#GSBResponse.ns01#GSBResult.ns0#Counts.*ns0#GBCContent map ((payload01, indexofPayload01) -> {
        (payload01.ns0#Values.*ns0#GBCV map ((payload02,indexofPayload02) -> {
        SEQ_NO: payload01.ns0#SEQ_NO,
                   DC: payload01.ns0#DC,
                   BNO: payload01.ns0#BNO,
                   BTYPE: payload01.ns0#BTYPE,
                   TNO: payload01.ns0#TNO,
                   RNO: payload01.ns0#RNO,
                   Code: payload02.ns0#Code,
                   Count: payload02.ns0#Count,
                   Amount: payload02.ns0#Amount
            }))
        }))
1
You should edit your question to include how your expected output is different from what you're getting. It's difficult to tell just looking at the payloads. Also, you should format your DW code so it can be easily understood. As of now, it's just a string of unformatted text. More info on that here: meta.stackexchange.com/questions/22186/… - jerney
updated the feed - Piyush Singh

1 Answers

0
votes

This is one possible solution:

%dw 1.0
%output application/json
%namespace b http://schemas.org/2004/07/DummyServ

%var content = payload.GSBResponse.GSBResult.b#Counts.*b#GBCContent
%var storeNumber = payload.GSBResponse.GSBResult.b#UID
---
content map using (seqNo = $.b#SEQ_NO, bno = $.b#BNO, btype = $.b#BTYPE, dc = $.b#DC, tno = $.b#TNO, rno = $.b#RNO) (
    ($.Values.*GBCV map {
        SEQ_NO: seqNo,
        DC: dc,
        BNO: bno,
        BTYPE: btype,
        TNO: tno,
        RNO: rno,
        Code: $.b#Code,
        Count: $.b#Count,
        Amount: $.b#Amount
    })
) reduce ($$ ++ $)

Here's the largest SEQ_NO:

%dw 1.0
%output application/java
%namespace b http://schemas.org/2004/07/DummyServ

---
max payload.GSBResponse.GSBResult.b#Counts.*b#GBCContent.b#SEQ_NO