Hi,
Is it possible to setup a function that only keeps lines/values that follow certain criteria?
For example, I receive a JSON with Clients contacts from different countries.
{
“Contact1”: “John - AT”,
“Contact2”: “John - DE”,
“Contact3”: “John - AT”,
“Contact4”: “John - ES”,
“Contact5”: “John - AT”
}
But I would only like to keep the “AT” ones, is it possible? I checked this one, but not making it happen so far: How to get the key value in a JSON file?
Thank you very much.
![:slight_smile: :slight_smile:](https://community.n8n.io/images/emoji/twitter/slight_smile.png?v=12)
The example below should do it.
{
"nodes": [
{
"parameters": {},
"name": "Start",
"type": "n8n-nodes-base.start",
"typeVersion": 1,
"position": [
250,
300
]
},
{
"parameters": {
"functionCode": "return [\n {\n json: {\n Contact1: \"John - AT\",\n Contact2: \"John - DE\",\n Contact3: \"John - AT\",\n Contact4: \"John - ES\",\n Contact5: \"John - AT\" \n } \n },\n]"
},
"name": "Function",
"type": "n8n-nodes-base.function",
"typeVersion": 1,
"position": [
560,
300
]
},
{
"parameters": {
"functionCode": "const results = {}\n\nconst data = items[0].json\n\nfor (const key of Object.keys(data)) {\n if (data[key].includes('AT')) {\n results[key] = data[key]\n }\n}\n\nreturn [ { json: results } ];"
},
"name": "Function1",
"type": "n8n-nodes-base.function",
"typeVersion": 1,
"position": [
810,
300
]
}
],
"connections": {
"Start": {
"main": [
[
{
"node": "Function",
"type": "main",
"index": 0
}
]
]
},
"Function": {
"main": [
[
{
"node": "Function1",
"type": "main",
"index": 0
}
]
]
}
}
}
1 Like
Awesome, thank you very much.