Contact Us 1-800-596-4880

Reference Multiple Inputs

DataWeave 2.1 is compatible with Mule 4.1. Standard Support for Mule 4.1 ended on November 2, 2020, and this version of Mule will reach its End of Life on November 2, 2022, 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 takes three different input JSON files, one in the payload, another in a variable and the third in an attribute. All of them are parts of the same Mule Event. The payload contains an array of book objects, the variable has a set of currency exchange rates, and the attribute specifies a year to be used as a query.

The example uses the following:

  • filter to leave out the books that are older than the date specified in the incoming attribute.

  • map to go through each book in the incoming array. Another map function then goes through the currencies in the variable to calculates the book’s price in each one. A third map function lists out all authors of a book in case there are more than one.

  • @ to define an XML attribute in the output.

DataWeave
%dw 2.0
output application/xml
---
books: {
  (payload filter ($.properties.year > attributes.publishedAfter) map  (item)   ->  {
    book @(year: item.properties.year): {
      (vars.exchangeRate.USD map {
        price @(currency: $.currency): $.ratio * item.price
      }),
      title: item.properties.title,
      authors: { (item.properties.author map {
        author: $
      }) }
    }
  } )
}
Input Payload
[
  {
      "type": "book",
      "price": 30,
      "properties": {
        "title": "Everyday Italian",
        "author": [
          "Giada De Laurentiis"
        ],
        "year": 2005
      }
  },
  {
      "type": "book",
      "price": 29.99,
      "properties": {
        "title": "Harry Potter",
        "author": [
          "J K. Rowling"
        ],
        "year": 2005
      }
  },
  {
      "type": "book",
      "price": 41.12,
      "properties": {
        "title": "Mule in Action",
        "author": [
          "David Dossot",
          "John D'Emic"
        ],
        "year": 2009
      }
  },
  {
      "type": "book",
      "price": 49.99,
      "properties": {
        "title": "XQuery Kick Start",
        "author": [
          "James McGovern",
          "Per Bothner",
          "Kurt Cagle",
          "James Linn",
          "Kurt Cagle",
          "Vaidyanathan Nagarajan"
        ],
        "year": 2003
      }
  },
  {
      "type": "book",
      "price": 39.95,
      "properties": {
        "title": "Learning XML",
        "author": [
          "Erik T. Ray"
        ],
        "year": 2003
      }
  }
]
Input Attributes
{
  "publishedAfter": 2004
}
Input Variable exchangeRate
{
  "USD": [
    {"currency": "EUR", "ratio":0.92},
    {"currency": "ARS", "ratio":8.76},
    {"currency": "GBP", "ratio":0.66}
  ]
}
Output
<?xml version='1.0' encoding='US-ASCII'?>
<books>
  <book year="2005">
    <price currency="EUR">27.60</price>
    <price currency="ARS">262.80</price>
    <price currency="GBP">19.80</price>
    <title>Everyday Italian</title>
    <authors>
      <author>Giada De Laurentiis</author>
    </authors>
  </book>
  <book year="2005">
    <price currency="EUR">27.5908</price>
    <price currency="ARS">262.7124</price>
    <price currency="GBP">19.7934</price>
    <title>Harry Potter</title>
    <authors>
      <author>J K. Rowling</author>
    </authors>
  </book>
  <book year="2009">
    <price currency="EUR">37.8304</price>
    <price currency="ARS">360.2112</price>
    <price currency="GBP">27.1392</price>
    <title>Mule in Action</title>
    <authors>
      <author>David Dossot</author>
      <author>John D'Emic</author>
    </authors>
  </book>
</books>