Filter JSON per keyword

Hi,
I am trying to apply rule based on a keyword as per this process: , but in separate

Starting from:

[
{
"places": "ITALY, ROME",
"place1": "ITALY, VENICE",
"place2": "ITALY, TURIN",
"place3": "SPAIN, MADRID",
"place4": "SPAIN, VALENCIA",
},
{
"places": "ROME, 10",
"place1": "MADRID, 20",
"place2": "TURIN, 20",
"place3": "LONDON, 10",
"place4": "PARIS, 15",
},
{
"places": "ROME, EUROPE",
"place1": "LONDON, EUROPE",
"place2": "NEW YORK, NORTH-AMERICA",
"place3": "MELBOURNE, OCEANIA",
"place4": "VALENCIA, EUROPE",
}
]

Searching for Keyword: “Rome” and only leaving those lines

Keyword: ROME

[
{
"places": "ITALY, ROME",
},
{
"places": "ROME, 10",
},
{
"places": "ROME, EUROPE",
}
]

Using

const results = {}
const data = items[0].json

for (const key of Object.keys(data)) {
  if (data[key].includes('Rome')) {
    results[key] = data[key]
  }
}

return [ { json: results } ];

Only returns 1st part due to “Item[]”, how to apply to all, was trying with foreach, but this JS is not my strong :confused:. Thank you very much.

Building on top of your code it would look like this:

const results = [];

items.forEach(item => {
  const data = item.json
  for (const key of Object.keys(item.json)) {
    if (data[key].toUpperCase().includes('Rome'.toUpperCase())) {
      results.push({ json: { [key]: data[key] } });
    }
  }
});

return results;

Here the example workflow:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "const results = [];\n\nitems.forEach(item => {\n  const data = item.json\n  for (const key of Object.keys(item.json)) {\n    if (data[key].toUpperCase().includes('Rome'.toUpperCase())) {\n      results.push({ json: { [key]: data[key] } });\n    }\n  }\n});\n\nreturn results;\n"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        350
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n\t{\n\t\tjson: {\n\t\t\t\"places\": \"ITALY, ROME\",\n\t\t\t\"place1\": \"ITALY, VENICE\",\n\t\t\t\"place2\": \"ITALY, TURIN\",\n\t\t\t\"place3\": \"SPAIN, MADRID\",\n\t\t\t\"place4\": \"SPAIN, VALENCIA\",\n\t\t}\n\t},\n\t{\n\t\tjson: {\n\t\t\t\"places\": \"ROME, 10\",\n\t\t\t\"place1\": \"MADRID, 20\",\n\t\t\t\"place2\": \"TURIN, 20\",\n\t\t\t\"place3\": \"LONDON, 10\",\n\t\t\t\"place4\": \"PARIS, 15\",\n\t\t}\n\t},\n\t{\n\t\tjson: {\n\t\t\t\"places\": \"ROME, EUROPE\",\n\t\t\t\"place1\": \"LONDON, EUROPE\",\n\t\t\t\"place2\": \"NEW YORK, NORTH-AMERICA\",\n\t\t\t\"place3\": \"MELBOURNE, OCEANIA\",\n\t\t\t\"place4\": \"VALENCIA, EUROPE\",\n\t\t}\n\t}\n]\n"
      },
      "name": "Mock Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        350
      ]
    }
  ],
  "connections": {
    "Mock Data": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like