# Merging / combine two arrays with objects

**URL:** <https://forum.alumio.com/t/merging-combine-two-arrays-with-objects/951>\
**Category:** Data Transformers\
**Created:** [January 24, 2024, 11:31am UTC](https://forum.alumio.com/t/merging-combine-two-arrays-with-objects/951 "2024-01-24T11:31:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniellefers](https://forum.alumio.com/user_avatar/forum.alumio.com/daniellefers/32/1563_2.png) [@daniellefers](https://forum.alumio.com/u/daniellefers)\
**Post date:** [January 24, 2024, 11:31am UTC](https://forum.alumio.com/t/merging-combine-two-arrays-with-objects/951/1 "2024-01-24T11:31:59Z")

</div>

I’m wondering how to merge two arrays with objects into a combined one based on a value available in both arrays.

I want `objectB.id` merged into `objectA` based on the value of `code`.

How can this be done?

Input:

```auto
{
    "objectA": [
        {
            "code": "key-A",
            "value": "value"
        },
        {
            "code": "Key-B",
            "value": "value"
        }
    ],
    "objectB": [
        {
            "code": "key-A",
            "id": "ID-A"
        },
        {
            "code": "Key-B",
            "id": "ID-B"
        }
    ]
}

```

Desired output

```auto
{
    "objectA": [
        {
            "code": "key-A",
            "value": "value",
            "id": "ID-A"
        },
        {
            "code": "Key-B",
            "value": "value",
            "id": "ID-B"
        }
    ],
    "objectB": [
        {
            "code": "key-A",
            "id": "ID-A"
        },
        {
            "code": "Key-B",
            "id": "ID-B"
        }
    ]
}

```

---

<div class="post-metadata">

**Author:** ![Alumio\_Bot](https://forum.alumio.com/user_avatar/forum.alumio.com/alumio_bot/32/1824_2.png) [@Alumio\_Bot](https://forum.alumio.com/u/Alumio_Bot)\
**Post date:** [January 24, 2024, 12:36pm UTC](https://forum.alumio.com/t/merging-combine-two-arrays-with-objects/951/2 "2024-01-24T12:36:43Z")

</div>

I see in your input; objectB is inconsistent.

```auto
        {
            "code": "key-A",
       ---> "id": "ID-A"
        },
        {
            "code": "Key-B",
       ---> "value": "ID-B"
        }

```

Should they both have the key “id”?

I think our JMESPath expert @Gugi could help.

---

<div class="post-metadata">

**Author:** ![daniellefers](https://forum.alumio.com/user_avatar/forum.alumio.com/daniellefers/32/1563_2.png) [@daniellefers](https://forum.alumio.com/u/daniellefers)\
**Post date:** [January 24, 2024, 12:44pm UTC](https://forum.alumio.com/t/merging-combine-two-arrays-with-objects/951/3 "2024-01-24T12:44:52Z")

</div>

You are right. Typo in my examples. I updated the examples in my post.

---

<div class="post-metadata">

**Author:** ![Gugi](https://forum.alumio.com/user_avatar/forum.alumio.com/gugi/32/236_2.png) [@Gugi](https://forum.alumio.com/u/Gugi)\
**Post date:** [January 25, 2024, 8:19am UTC](https://forum.alumio.com/t/merging-combine-two-arrays-with-objects/951/4 "2024-01-25T08:19:45Z")

</div>

Hi @daniellefers @Alumio_Bot

Unfortunately, it’s not something that can be practically done using JMESPath. There are four steps we should do to achieve the objective.

First, we need to group both array by each object’s `code` using [Group records](https://forum.alumio.com/t/how-to-group-records/279).

 ![image](https://alumio-forum.s3.dualstack.eu-central-1.amazonaws.com/uploads/original/2X/3/3da85756082cde2627c79caf8d76e6422d869a48.png)

It will result in the below JSON where the objects in each array will be grouped in a property that matches the object’s `code`.

```auto
{
  "objectA": {
    "key-A": [
      {
        "code": "key-A",
        "value": "value"
      }
    ],
    "Key-B": [
      {
        "code": "Key-B",
        "value": "value"
      }
    ]
  },
  "objectB": {
    "key-A": [
      {
        "code": "key-A",
        "id": "ID-A"
      }
    ],
    "Key-B": [
      {
        "code": "Key-B",
        "id": "ID-B"
      }
    ]
  }
}

```

Next, assuming that each `code` can only have one object in `objectA` and one object in `objectB`, you can use [Copy using a pattern](https://forum.alumio.com/t/copy-using-a-pattern/276) to copy the objects in `objectB` to `objectA` according to their `code`.

 ![image](https://alumio-forum.s3.dualstack.eu-central-1.amazonaws.com/uploads/original/2X/a/addefc63d5c42f60e086c26cfdb484afbc263605.png)

Below is the result from the transformer.

```auto
{
  "objectA": {
    "key-A": [
      {
        "code": "key-A",
        "value": "value"
      },
      {
        "code": "key-A",
        "id": "ID-A"
      }
    ],
    "Key-B": [
      {
        "code": "Key-B",
        "value": "value"
      },
      {
        "code": "Key-B",
        "id": "ID-B"
      }
    ]
  },
  "objectB": {
    "key-A": [
      {
        "code": "key-A",
        "id": "ID-A"
      }
    ],
    "Key-B": [
      {
        "code": "Key-B",
        "id": "ID-B"
      }
    ]
  }
}

```

Then, we can now merge the objects with the same `code` in `objectA` so that there will be one object for each `code` and has properties `code`, `value` and `id`. We can use [Value mapper](https://forum.alumio.com/t/how-to-use-value-mapper/290) to do so with a help from Operator mapper + Array Merge.

 ![image](https://alumio-forum.s3.dualstack.eu-central-1.amazonaws.com/uploads/original/2X/5/5d11b021ba34b74220393d087dc5374fdb8f4fef.png)

We will now see that we are close to our objective, but the `objectA` is currently an object (associative array).

```auto
{
  "objectA": {
    "key-A": {
      "code": "key-A",
      "value": "value",
      "id": "ID-A"
    },
    "Key-B": {
      "code": "Key-B",
      "value": "value",
      "id": "ID-B"
    }
  },
  "objectB": {
    "key-A": [
      {
        "code": "key-A",
        "id": "ID-A"
      }
    ],
    "Key-B": [
      {
        "code": "Key-B",
        "id": "ID-B"
      }
    ]
  }
}

```

In order to convert it to an indexed array, we will need another Value mapper. But this time, the mapper we will use is “List: Get values”.

 ![image](https://alumio-forum.s3.dualstack.eu-central-1.amazonaws.com/uploads/original/2X/f/fedcf820636e8e8c680e5ddd98093e5834853297.png)

Finally, we will get our desired output.

```auto
{
  "objectA": [
    {
      "code": "key-A",
      "value": "value",
      "id": "ID-A"
    },
    {
      "code": "Key-B",
      "value": "value",
      "id": "ID-B"
    }
  ],
  "objectB": {
    "key-A": [
      {
        "code": "key-A",
        "id": "ID-A"
      }
    ],
    "Key-B": [
      {
        "code": "Key-B",
        "id": "ID-B"
      }
    ]
  }
}

```

Please give it a try and let us know if it doesn’t work as expected.

---

<div class="post-metadata">

**Author:** ![daniellefers](https://forum.alumio.com/user_avatar/forum.alumio.com/daniellefers/32/1563_2.png) [@daniellefers](https://forum.alumio.com/u/daniellefers)\
**Post date:** [February 7, 2024, 2:01pm UTC](https://forum.alumio.com/t/merging-combine-two-arrays-with-objects/951/5 "2024-02-07T14:01:40Z")

</div>

Works like a charm! Challenge completed! 😉
