In this Article, we will see how we can optimize and allow Json data to accept and transform duplicate elements. In real-time projects optimizing data really matters when we talk about tuning performance. Either you say in terms of  logging data or in terms of processing data every byte matters.

In DataWeave, we have two properties which will hep us to optimize our application data.

  • duplicateKeyAsArray: JSON data does not allow duplicate keys within same parent, so we may get exception in real-time project development scenario. With this attribute, if your dataweave finds any duplicate keys, it will create an array with all those values.

Sample XML Input:

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

Dataweave Script:

%dw 2.0
output application/json duplicateKeyAsArray=true

payload

Sample Output:

{
  “users”: {
    “user”: {
      “personal_information”: {
        “first_name”: [
        “Manish”,
        “MaK”
        ],
        “middle_name”: “Kumar”,
        “last_name”: “Sah”
      }
    }
  }
}

  • indent: It indicates whether you want to compress your JSON data into a single line or you need JSON data for better readability.
Sample Input:
<users>
 <user>
  <personal_information>
   <first_name version=”V1″>Manish</first_name>
   <first_name version=”V1″>Mak</first_name>
   <middle_name>Kumar</middle_name>
   <last_name>Sah</last_name>
  </personal_information>
 </user>
</users>

Dataweave Script:

%dw 2.0
output application/json duplicateKeyAsArray=true, indent=false

payload

Sample Output:

{“users”: {“user”: {“personal_information”: {“first_name”: [“Manish”,”MaK”],”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.

4 thoughts on “Optimize Json data using DataWeave”
  1. Hiya, I’m really glad I’ve found this info. Today bloggers publish just about gossips and net and this is really annoying. A good blog with interesting content, this is what I need. Thanks for keeping this web-site, I will be visiting it. Do you do newsletters? Cant find it.

  2. Oh my goodness! an amazing article dude. Thank you However I am experiencing issue with ur rss . Don?t know why Unable to subscribe to it. Is there anyone getting identical rss problem? Anyone who knows kindly respond. Thnkx

Leave a Reply

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