Merging / combine two arrays with objects

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:

{
    "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

{
    "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"
        }
    ]
}

I see in your input; objectB is inconsistent.

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

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

Hi @daniellefers @Roland

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.

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.

{
  "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 to copy the objects in objectB to objectA according to their code.

Below is the result from the transformer.

{
  "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 to do so with a help from Operator mapper + Array Merge.

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

{
  "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”.

Finally, we will get our desired output.

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

Works like a charm! Challenge completed! :wink:

1 Like