Mulesoft 4 Transformation – Convert JSON to XML

We will start creating Mule Project in Anypoint studio and input would be provided by Http Listener.

Make sure MIME type of Http Listener set to application/json, otherwise we will get below error :

Dataweave Error: You called the function ‘Value Selector’ with these arguments …

Mulesoft Project structure below

Mulesoft-Project-Convert-JSON-to-XML
Mulesoft Project – Convert JSON to XML

Method 1 : Simplest

Simplest way to convert JSON to XML is to just put payload as value in transformation component

%dw 2.0
output application/xml
---
{
	Profiles: payload
}

Input (You can use Postman to make POST request on Http listener)

{
    "Profiles" : [
                    {
                        "name":"John",
                        "LastName" : "Doe",
                        "Age" : 30,
                        "Country":"India",
                        "Profession" : "Salesofrce Architect",
                        "Skills":"Salesforce, HTML"
                    },
                    {
                        "name":"Jenny",
                        "LastName" : "Doe",
                        "Age" : 40,
                        "Country":"USA",
                        "Profession" : "Mulesoft Architect",
                        "Skills":"HTML, Mulesoft"
                    }
                ]
    }

Output

<?xml version='1.0' encoding='UTF-8'?>
<Profiles>
    <Profiles>
        <name>John</name>
        <LastName>Doe</LastName>
        <Age>30</Age>
        <Country>India</Country>
        <Profession>Salesofrce Architect</Profession>
        <Skills>Salesforce, HTML</Skills>
    </Profiles>
    <Profiles>
        <name>Jenny</name>
        <LastName>Doe</LastName>
        <Age>40</Age>
        <Country>USA</Country>
        <Profession>Mulesoft Architect</Profession>
        <Skills>HTML, Mulesoft</Skills>
    </Profiles>
</Profiles>

As you can see above, JSON converted to XML with almost same format. If it works for you then great. However you may want some flexibility on how final xml look like.

Method 2 – Changing format while conversion

If we want lastName and age as attribute of name node in xml, then we will need to play little bit with Dataweave2 as shown below

%dw 2.0
output application/xml
---
{
	Profiles: {
		(payload.Profiles map ( person , indexOfPerson ) -> {
			candidates: {
				name @(lastName: person.LastName , age: person.Age): person.name,
				country: person.Country,
				Profession: person.Profession,
				Skills: person.Skills
			}
		})
	}
}

Output :

<?xml version='1.0' encoding='UTF-8'?>
<Profiles>
    <candidates>
        <name lastName="Doe" age="30">John</name>
        <country>India</country>
        <Profession>Salesofrce Architect</Profession>
        <Skills>Salesforce, HTML</Skills>
    </candidates>
    <candidates>
        <name lastName="Doe" age="40">Jenny</name>
        <country>USA</country>
        <Profession>Mulesoft Architect</Profession>
        <Skills>HTML, Mulesoft</Skills>
    </candidates>
</Profiles>

Posted

in

by


Related Posts

Comments

2 responses to “Mulesoft 4 Transformation – Convert JSON to XML”

  1. amol Avatar
    amol

    thanks for the information.
    in my case, I have following format to be generated for which I am facing difficulty.

    abc

  2. Simon Avatar
    Simon

    Hello. You can use a free online tool to convert JSON to XML https://freetools.site/data-converters/json-to-xml

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading