In project development sometimes we needed to convert the payload from xml to json. We also need to write the xml attributes after transformation. 

For this purpose we can use writeAttributes=true in header section of data-weave. This attribute is available from Dataweave2.


Please find the snippets below:


Sample XML Input:

<users>
<user>
<personal_information>
<first_name version=”V1″>Manish</first_name>
<middle_name>Kumar</middle_name>
<last_name>Sah</last_name>
</personal_information>
</user>
</users>

Dataweave Script

%dw 2.0
output application/json writeAttributes=true

payload

Output:

{
  “users”: {
    “user”: {
      “personal_information”: {
        “first_name”: {
          “@version”: “V1“,
          “__text”: “Manish”
        },
        “middle_name”: “Kumar”,
        “last_name”: “Sah”
      }
    }
  }
}

Happy Learning 🙂

By Manish Kumar

I am having around 10 years of IT experience in Integration Architecture, Requirement gathering, Effort Estimation, Application Design\Development\Testing and Deployment including 5+ years of experience in MuleSoft ESB and Hybrid Integrations. DevOps and Cloud Integration is my area of interest.

2 thoughts on “How to write Attributes from XML to Json – DW2”

Leave a Reply

Your email address will not be published. Required fields are marked *