---
schema_version: "1"
id: 9a62eb683f5a89919d729ae0c2f9cbf0d63133456b1d186798b51d8cab15409f
title: Merge Fields from Separate Objects
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.3/dataweave-cookbook-merge-multiple-payloads.md
canonical_url: https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-merge-multiple-payloads
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 7e881527ddaf70bb44ddfee8f02f7e57553f80edef3cc51116b774017be7c332
description: MuleSoft Documentation Site
release: "2.3"
latest: false
---
---
product: DataWeave
version: 2.3
keywords: studio, anypoint, transform, transformer, format, aggregate, filter, json, metadata, dataweave, data weave, datamapper, dwl, dfl, dw, output structure, input structure, map, mapping, contains, as
---

# Merge Fields from Separate Objects

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

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.

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:

```dataweave
%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
    })
  }
```

Output JSON:

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

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:

```dataweave
%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
    })
  }
```

Output JSON:

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

Both scripts produce the same output.

## Related Examples

- [Extract Data](https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-extract-data)

- [Reference Multiple Inputs](https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-reference-multiple-inputs)

- [Zip Arrays Together](https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-zip-arrays-together)

## See Also

- [DataWeave Selectors](https://docs.mulesoft.com/dataweave/2.3/dataweave-selectors)

- [DataWeave Cookbook](https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook)
