How to use Dataweave to transform XML to JSON

As more and more companies move towards a service-oriented architecture, the need to transform data from one format to another has become increasingly important. One such format is XML, which is still widely used for data exchange between different systems. However, JSON has gained popularity in recent years due to its lightweight and easy-to-parse nature. In this blog, we will explore how to use Dataweave to transform XML to JSON.

Dataweave is a powerful data transformation language used in MuleSoft’s Anypoint Platform for building integrations. It provides a simple and intuitive way to transform data between different formats such as XML, JSON, and CSV. Dataweave uses a functional programming paradigm and is based on the Mule Expression Language (MEL).

To transform XML to JSON using Dataweave, we first need to define the input XML structure and the desired output JSON structure. Let’s take a simple example of an XML file that contains information about books.

<?xml version="1.0" encoding="UTF-8"?>
<books>
  <book>
    <title>The Alchemist</title>
    <author>Paulo Coelho</author>
    <year>1988</year>
  </book>
  <book>
    <title>The Catcher in the Rye</title>
    <author>J.D. Salinger</author>
    <year>1951</year>
  </book>
</books>

Our goal is to transform this XML file into the following JSON format:

[
  {
    "title": "The Alchemist",
    "author": "Paulo Coelho",
    "year": "1988"
  },
  {
    "title": "The Catcher in the Rye",
    "author": "J.D. Salinger",
    "year": "1951"
  }
]

To achieve this, we can use the following Dataweave code:

%dw 2.0
output application/json
---
payload.books.*book map {
  title: $.title,
  author: $.author,
  year: $.year
}

Let’s break down this code:

  • %dw 2.0 specifies the Dataweave version we are using.
  • output application/json specifies the desired output format.
  • payload.books.*book selects all the book elements under the books element in the input XML.
  • map is a Dataweave function that applies the transformation logic to each element in the input.
  • { title: $.title, author: $.author, year: $.year } defines the output JSON structure. $ represents the current element being transformed.

When we run this code on the input XML, we get the following JSON output:

[
  {
    "title": "The Alchemist",
    "author": "Paulo Coelho",
    "year": "1988"
  },
  {
    "title": "The Catcher in the Rye",
    "author": "J.D. Salinger",
    "year": "1951"
  }
]

As you can see, Dataweave has successfully transformed the input XML into the desired JSON format.

Let’s take another example where the XML contains nested elements. Consider the following input XML:

<?xml version="1.0" encoding="UTF-8"?>
<employees>
  <employee>
    <name>John Doe</name>
    <department>
      <name>HR</name>
      <location>New York</location>
    </department>
    <salary>50000</salary>
  </employee>
  <employee>
    <name>Jane Doe</name>
    <department>
      <name>Marketing</name>
      <location>San Francisco</location>
    </department>
    <salary>60000</salary>
  </employee>
</employees>

Our goal is to transform this XML into the following JSON format:

[
  {
    "name": "John Doe",
    "department": {
      "name": "HR",
      "location": "New York"
    },
    "salary": "50000"
  },
  {
    "name": "Jane Doe",
    "department": {
      "name": "Marketing",
      "location": "San Francisco"
    },
    "salary": "60000"
  }
]

To achieve this, we can use the following Dataweave code:

%dw 2.0
output application/json
---
payload.employees.*employee map {
  name: $.name,
  department: {
    name: $.department.name,
    location: $.department.location
  },
  salary: $.salary
}

Let’s break down this code:

  • %dw 2.0 specifies the Dataweave version we are using.
  • output application/json specifies the desired output format.
  • payload.employees.*employee selects all the employee elements under the employees element in the input XML.
  • map is a Dataweave function that applies the transformation logic to each element in the input.
  • { name: $.name, department: { name: $.department.name, location: $.department.location }, salary: $.salary } defines the output JSON structure. $ represents the current element being transformed. Note that we are using nested JSON objects to represent the department element in the output.

When we run this code on the input XML, we get the following JSON output:

[
  {
    "name": "John Doe",
    "department": {
      "name": "HR",
      "location": "New York"
    },
    "salary": "50000"
  },
  {
    "name": "Jane Doe",
    "department": {
      "name": "Marketing",
      "location": "San Francisco"
    },
    "salary": "60000"
  }
]

As you can see, Dataweave has successfully transformed the input XML into the desired JSON format.

For more details check the official documentation: Transform XML to JSON

In conclusion, Dataweave provides a simple and intuitive way to transform XML to JSON. With its functional programming paradigm and support for different data formats, Dataweave can help developers build integrations that seamlessly exchange data between different systems.

More from the blog

Handling Dates and Times in Dataweave

Dataweave is a powerful data transformation language used in MuleSoft to transform data from one format to another. When working with data, one of...

Using MuleSoft to Implement Content-Based Routing (Choice Router)

Content-based routing is a widely used architectural pattern that is particularly useful for handling incoming messages or requests that need to be distributed based...

Hash Indexing in RDBMS

In relational database management systems (RDBMS), indexing is an essential feature that allows for faster retrieval of data. A hash index is a type...

Caching in RDBMS

Caching is a technique that stores frequently used data in memory for faster access. The goal of caching is to reduce the time it...