# 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:** 1\
**Showing post:** 4

<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.

---

_[View the full topic](https://forum.alumio.com/t/merging-combine-two-arrays-with-objects/951)._
