Merge Fields from Separate Objects

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.

The DataWeave examples merge fields from separate input arrays. The first (firstInput) is a DataWeave variable that lists price by book ID, and the second (secondInput) lists authors by book ID. Each book has a unique bookId key. 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 DataWeave scripts produce the same output. Both scripts use two map functions with a filter, and one of them also creates an alias for the bookId:

  • The first map function iterates over the elements of the firstInput array. As the function evaluates each element, a second map function uses a filter to identify any elements in the second array (secondInput) that match the filter criteria (secondInput filter ($.*bookId contains firstInputValue.bookId)). The filter returns an element from secondInput that contains a bookId value that matches the bookId value in the firstInput element.

  • The second map function evaluates that filtered element and then uses secondInputValue.author to select and populate the value of its "author" field in the object {author : secondInputValue.author}.

  • filter limits the scope of the second map function to objects in the secondInput that share the same bookId.

DataWeave Script:
%dw 2.0
var firstInput = [
  { "bookId":"101",
    "title":"world history",
    "price":"19.99"
  },
  {
    "bookId":"202",
    "title":"the great outdoors",
    "price":"15.99"
  }
]
var secondInput = [
  {
    "bookId":"101",
    "author":"john doe"
  },
  {
    "bookId":"202",
    "author":"jane doe"
  }
]
output application/json
---
firstInput map (firstInputValue) ->
  {
    theId : firstInputValue.bookId as Number,
    theTitle: firstInputValue.title,
    thePrice: firstInputValue.price as Number,
    (secondInput filter ($.*bookId contains firstInputValue.bookId) map (secondInputValue) -> {
      theAuthor : secondInputValue.author
    })
  }

As the next script shows, you can also write the same script using an id alias (created with using (id = firstInputValue.bookId)). The alias replaces the selector expression, firstInputValue.bookId, which is longer.

DataWeave Script with Alias:
%dw 2.0
var firstInput = [
  { "bookId":"101",
    "title":"world history",
    "price":"19.99"
  },
  {
    "bookId":"202",
    "title":"the great outdoors",
    "price":"15.99"
  }
]
var secondInput = [
  {
    "bookId":"101",
    "author":"john doe"
  },
  {
    "bookId":"202",
    "author":"jane doe"
  }
]
output application/json
---
firstInput map (firstInputValue) -> using (id = firstInputValue.bookId)
  {
    theValue : id as Number,
    theTitle: firstInputValue.title,
    thePrice: firstInputValue.price as Number,
    (secondInput filter ($.*bookId contains id)  map (secondInputValue) -> {
      theAuthor : secondInputValue.author
    })
  }

Both scripts produce the same output.

Output
[
  {
    "theId": 101,
    "theTitle": "world history",
    "thePrice": 19.99,
    "theAuthor": "john doe"
  },
  {
    "theId": 202,
    "theTitle": "the great outdoors",
    "thePrice": 15.99,
    "theAuthor": "jane doe"
  }
]

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub