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.