user9144014
user9144014

Reputation: 3

Transforming XML to JSON using dataweave

Request to please help me with the dataweave to achieve below output. Here we need to do the following.

  1. We need to add default values when XML element is null.
  2. We need to map all the repeating elements inside XML element to their corresponding parent XML element.

I tried writing the dataweave for the same but was not able to achieve the desired response.

Request

<PacketResponse>
    <PackResult>
        <Result>0</Result>
        <packs>
            <pack>
                <SeqNo>1</SeqNo>
                <PackNumber>2550082245045</PackNumber>
                <packageDate>2022-07-08T13:31:55.917+02:00</packageDate>
                <Values i:nil="true"/>
            </pack>       
            <pack>
                <SeqNo>2</SeqNo>
                <PackNumber>2550256260676</PackNumber>
                <packageDate>2022-07-27T08:36:18.12+02:00</packageDate>
                <Values>
                    <GetPackValue>
                        <Code>000228</Code>
                        <Count>80</Count>
                        <Amount>1600.00</Amount>
                    </GetPackValue>
                    <GetPackValue>
                        <Code>000227</Code>
                        <Count>20</Count>
                        <Amount>200.00</Amount>
                    </GetPackValue>
                    <GetPackValue>
                        <Code>000229</Code>
                        <Count>40</Count>
                        <Amount>2000.00</Amount>
                    </GetPackValue>
                </Values>
            </pack>
            <pack>
                <SeqNo>3</SeqNo>
                <PackNumber>2550326072025</PackNumber>
                <packageDate>2022-08-09T13:17:43.197+02:00</packageDate>
                <Values>
                    <GetPackValue>
                        <Code>000240</Code>
                        <Count>5</Count>
                        <Amount>260.00</Amount>
                    </GetPackValue>
                </Values>
            </pack>
        </packs>
    </PackResult>
</PacketResponse>

Expected Response

[
  {
    "seqNo": "1",
    "PackNumber": "2550082245045",
    "packageDate": "2022-07-08 01:31:55",
    "code": "000000",
    "count": 0,
    "amount": 0
  },
  {
    "seqNo": "2",
    "PackNumber": "2550256260676",
    "code": "000228",
    "packageDate": "2022-07-27 08:36:18",
    "count": "80",
    "amount": 1600.00
  },
  {
    "seqNo": "2",
    "PackNumber": "2550256260676",
    "code": "000227",
    "packageDate": "2022-07-27 08:36:18",
    "count": "20",
    "amount": 200.00
  },
  {
    "seqNo": "2",
    "PackNumber": "2550256260676",
    "code": "000229",
    "packageDate": "2022-07-27 08:36:18",
    "count": "40",
    "amount": 2000.00
  },
  {
    "seqNo": "3",
    "PackNumber": "2550326072025",
    "code": "000240",
    "packageDate": "2022-08-09 01:17:43",
    "count": "5",
    "amount": 260.00
  }
]

Upvotes: 0

Views: 393

Answers (3)

user19840120
user19840120

Reputation: 1

Use multivalue selector and apply map on it, like mentioned below:

   ' %dw 2.0
output application/json
---
payload.PacketResponse.PackResult.*packs.*pack map ((item, index) -> {
seqNo: item.SeqNo,
PackNumber: item.PackNumber,
packageDate: item.packageDate,
code: item.Values.GetPackValue.Code default "",
count: item.Values.GetPackValue.Count default "",
amount: item.Values.GetPackValue.Amount default "
} ) '

Upvotes: 0

machaval
machaval

Reputation: 5059

I've used a combination of flatMap() and multi-value selector:

%dw 2.0
output application/json
---
payload.PacketResponse.PackResult.packs.*pack flatMap ((pack, index) -> do {
        var packResult = {
            seqNo: pack.SeqNo,
            "PackNumber": pack.PackNumber,
            "packageDate": pack.packageDate,        
        }
        ---
        pack.Values.*GetPackValue map ((packValue, index) -> packResult ++ packValue)                   default ([packResult ++ {"code": "000000", "count": 0,"amount": 0}])
    }
)

Upvotes: 1

Shoki
Shoki

Reputation: 1538

Here's an alternative way to do the same.

The key here is that you need to iterate for each of the GetPackValues but make sure to generate a value for the cases where there's no value too. Here the pack.Values.*GetPackValue default [{}] takes care of that.

Finally since you have a nested Array because some Values have multiple GetPackValues, the flatMap takes care of flattening those results into a simple Array.

%dw 2.0
output application/json
---
payload.PacketResponse.PackResult.packs.*pack flatMap (pack) -> 
    pack.Values.*GetPackValue default [{}] map (value) -> {
        "seqNo": pack.SeqNo,
        "PackNumber": pack.PackNumber,
        "packageDate": pack.packageDate,
        "code": value.Code default "000000",
        "count": value.Count default 0,
        "amount": value.Amount default 0
    }

Upvotes: 1

Related Questions