I would like to join 2 arrays having about 500 elements in a performant way using JSONata.
I have found a way to join 2 arrays but it is not very performant. See https://try.jsonata.org/VqzeZDAjA (The same input/output and query is copied here below).
Input :
{
"_msgid": "a070e32c.e71ed",
"topic": "",
"rc": {
"code": 0
},
"table1": {
"array1": [
{
"country_region": "Thailand",
"field_A": "A for Thailand"
},
{
"country_region": "Japan",
"field_A": "A for Japan"
}
]
},
"array2": [
{
"country_region": "Thailand",
"field_B": "B for Thailand"
},
{
"country_region": "Japan",
"field_B": "B for Japan"
}
]
}
Expected output:
{
"array1": [
{
"country_region": "Thailand",
"field_A": "A for Thailand",
"field_B": "B for Thailand"
},
{
"country_region": "Japan",
"field_A": "A for Japan",
"field_B": "B for Japan"
}
]
}
Working query but not very performant in case of arrays with 500 elements.
(
$array2 := array2;
table1 ~> | array1 | { "field_B" :
($country_region := country_region;
$array2[$country_region=$.country_region])
.field_B}|
)
Update 2020-03-29
Here above it is claimed that the Working query (see above) is not very performant. Further analysis revealed that this not true: the actual performance of the above query is fine (similar to the performance of the approved query). The performance issue I encountered was caused by another JSONata query that has nothing to do with respect to this join !
Not sure if it's any more performant, but I'd use the join syntax to do this:
{
"array1": array2@$A2.table1.array1@$A1[$A1.country_region = $A2.country_region].$merge([$A1, $A2])
}