Map Object Elements as an Array

DataWeave 2.2 is compatible and bundled with Mule 4.2. Standard Support for Mule 4.2 ended on May 2, 2021, and this version of Mule will reach its End of Life on May 2, 2023, when Extended Support ends.

Deployments of new applications to CloudHub that use this version of Mule are no longer allowed. Only in-place updates to applications are permitted.

MuleSoft recommends that you upgrade to the latest version of Mule 4 that is in Standard Support so that your applications run with the latest fixes and security enhancements.

This DataWeave example uses the DataWeave map function to iterate through the object elements that match the key book. The input also includes the key magazine, which is ignored. Before you begin, note that DataWeave version 2 (%dw 2.0) is for Mule 4 apps. For a Mule 3 app, refer to DataWeave 1.0 (%dw 1.0) examples, within the Mule 3.9 documentation set. For other Mule versions, you can use the Mule Runtime version selector in the table of contents.

The example uses these DataWeave functions:

  • The multi-value selector *book to return an array with the elements that match the key "book".

  • map to go through each object in the array that’s returned by the multi-value selector.

DataWeave
%dw 2.0
var myInputExample = {
  "inventory": {
      "book" : {
        "category": "cooking",
        "title": "Everyday Italian",
        "author": "Giada De Laurentiis",
        "year": "2005",
        "price": "30.00"
      },
      "book" :{
        "category": "children",
        "title": "Harry Potter",
        "author": "J K. Rowling",
        "year": "2005",
        "price": "29.99"
      },
      "book" :{
        "category": "web",
        "title": "Learning XML",
        "author": "Erik T. Ray",
        "year": "2003",
        "price": "39.95"
      },
      "magazine" :{
        "category": "web",
        "title": "Wired Magazine",
        "edition": "03-2017",
        "price": "15.95"
      },
        "magazine" :{
        "category": "business",
        "title": "Time Magazine",
        "edition": "04-2017",
        "price": "17.95"
      }
   }
}
output application/json
---
items: myInputExample.inventory.*book map (item, index) -> {
      "theType": "book",
      "theID": index,
      "theCategory": item.category,
      "theTitle": item.title,
      "theAuthor": item.author,
      "theYear": item.year,
      "thePrice": item.price as Number
}
Output
{
  "items": [
    {
      "theType": "book",
      "theID": 0,
      "theCategory": "cooking",
      "theTitle": "Everyday Italian",
      "theAuthor": "Giada De Laurentiis",
      "theYear": "2005",
      "thePrice": 30.00
    },
    {
      "theType": "book",
      "theID": 1,
      "theCategory": "children",
      "theTitle": "Harry Potter",
      "theAuthor": "J K. Rowling",
      "theYear": "2005",
      "thePrice": 29.99
    },
    {
      "theType": "book",
      "theID": 2,
      "theCategory": "web",
      "theTitle": "Learning XML",
      "theAuthor": "Erik T. Ray",
      "theYear": "2003",
      "thePrice": 39.95
    }
  ]
}

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub