Dataweave script or function to transform unordered JSON payload to ordered XML payload?

51 Views Asked by At

I am trying to transform a JSON payload that has no set ordering to an XML payload that is defined by an XSD schema. Is there any DataWeave script or function that can dynamically map elements to conform to that XSD? In essence my JSON payload has the same structure of the SOAP body that I have but there might be chances that the ordering of elements in the JSON request may be out of order.

For example: JSON input:

{
    "ConcatenateRequest":
    {
        "string2": "World",
        "string1": "Hello"
    }
}

XML output after transformation:

    <ConcatenateRequest>
        <string1>Hello</string1>
        <string2>World</string2>
    </ConcatenateRequest>

assuming that the order/schema has string1 appear before string2

1

There are 1 best solutions below

0
ITroubs On

This dataweave contains two ways of ordering: One is by the value the other one by key. Both result in the same output but that is due to the coincidence in the data provided.

%dw 2.0
output application/json
---
{
  "ConcatenateRequestByVlaue": payload.ConcatenateRequest orderBy ((value, key) -> value),
  "ConcatenateRequestByKey": payload.ConcatenateRequest orderBy ((value, key) -> key)
}