Contact Us 1-800-596-4880

Reference Multiple Inputs

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. Before you begin, note that DataWeave version 2 (%dw 2.0) is for Mule 4 apps. For a Mule 3 app, refer to DataWeave version 1 (%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 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 Script:
%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 JSON 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:
<?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>