cancel
Showing results for 
Search instead for 
Did you mean: 

Filter json array of objects by another json array of objects

vfantidou
New Contributor

Hello everyone,

I have a json that looks like the bellow and I want to filter the $input0 data by whatever key/values exists in $filterData in the end. The $filterData may have ID or Customer (in this example it has ID). I have tried in Mapper with filter() and in Filter Snap, I did not have any success. Can someone help or had to solve something similar?

[
{
"input0": [
{
"ID": "1",
"CUSTOMER": "a"
},
{
"ID_NUMERIC": "2",
"CUSTOMER": "b"
},
{
"ID": "1",
"CUSTOMER": "c"
},
{
"ID": "3",
"CUSTOMER": "d"
},
{
"ID": "4",
"CUSTOMER": "f"
},
{
"ID": "4",
"CUSTOMER": "b"
}
],
"filterData": [
{
"ID": "1"
},
{
"ID": "2"
}
]
}
]

 

The output should contain both ID and Customer values, just like the $input0 but filtered.

1 ACCEPTED SOLUTION

Supratim
Contributor III

@vfantidou  Please use below expression-. From your example seems there are some typo (2nd record in input0 should have ID not ID_NUMERIC). If it's not typo you need make null safe in mapper.

input0.filter((val,index)=> $filterData.find((ele)=> ele['ID'] ==val['ID'] ) != null )

View solution in original post

1 REPLY 1

Supratim
Contributor III

@vfantidou  Please use below expression-. From your example seems there are some typo (2nd record in input0 should have ID not ID_NUMERIC). If it's not typo you need make null safe in mapper.

input0.filter((val,index)=> $filterData.find((ele)=> ele['ID'] ==val['ID'] ) != null )