Trying to filter a json with another json

I am trying to filter a json with another json, to get the values which are present in both of them

Please share the workflow

```{
  "nodes": [
    {
      "parameters": {
        "filePath": "/home/n8n/Download/vergleich.json"
      },
      "name": "Read Binary File",
      "type": "n8n-nodes-base.readBinaryFile",
      "typeVersion": 1,
      "position": [
        -20,
        60
      ]
    },
    {
      "parameters": {
        "filePath": "/home/n8n/Download/empirum.json"
      },
      "name": "Read Binary File1",
      "type": "n8n-nodes-base.readBinaryFile",
      "typeVersion": 1,
      "position": [
        -20,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const empirumImport = new Array ($items(\"empirumFunction\"));\nconst dellImport = new Array($items(\"dellFunction\"));\n\n\n\nvar haveDevice = dellImport.filter(function(dell){\n    return empirumImport.filter(function(emp){\n        return dell.Modell == emp.Name;\n\n    })\n\n});\nreturn haveDevice;\nconsole.log(haveDevice);\n\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        540,
        140
      ]
    },
    {
      "parameters": {
        "options": {
          "encoding": "utf8"
        }
      },
      "name": "dellImport",
      "type": "n8n-nodes-base.moveBinaryData",
      "typeVersion": 1,
      "position": [
        120,
        60
      ]
    },
    {
      "parameters": {
        "options": {
          "encoding": "utf8"
        }
      },
      "name": "empirumImport",
      "type": "n8n-nodes-base.moveBinaryData",
      "typeVersion": 1,
      "position": [
        120,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const result = []\nfor (item of items) {\n  for (json of item.json) {\n     result.push({\n        json: {...json }\n     })\n  }\n}\n\nreturn result"
      },
      "name": "empirumFunction",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        260,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const result = []\nfor (item of items) {\n  for (json of item.json) {\n     result.push({\n        json: {...json }\n     })\n  }\n}\n\nreturn result"
      },
      "name": "dellFunction",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        260,
        60
      ]
    }
  ],
  "connections": {
    "Read Binary File": {
      "main": [
        [
          {
            "node": "dellImport",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Binary File1": {
      "main": [
        [
          {
            "node": "empirumImport",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "dellImport": {
      "main": [
        [
          {
            "node": "dellFunction",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "empirumImport": {
      "main": [
        [
          {
            "node": "empirumFunction",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "empirumFunction": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "dellFunction": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

(Select the nodes and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow respectively)


## Share the output returned by the last node
<!-- If you need help with transforming the data, please also share the expected output -->
[

{

"json": {

"Modell": "Latitude 9520",

"Version": "A02",

"Link": "valid https link to dell website."

}

},

{

"json": {

"Modell": "Latitude 9510",

"Version": "A06",

"Link": "valid https link to dell website."

}

},

{

"json": {

"Modell": "Latitude 9420",

"Version": "A05",

"Link": "valid https link to dell website."

}

},

{

"json": {

"Modell": "Latitude 9410",

"Version": "A08",

"Link": "https://www .dell.com/s upport/kbdoc/ 000109721/la titude-9410 -window s-10-driver-pack"

}

},

{

"json": {

"Modell": "Latitude 7520",

"Version": "A03",

"Link": "valid https link to dell website."
 
}

},

{

"json": {

"Modell": "Latitude 7490",

"Version": "A15",

"Link": "valid https link to dell website."

}

},

{

"json": {

"Modell": "Latitude 7480",

"Version": "A15",

"Link": "valid https link to dell website."

}

}
]

]


The input data for dellFunction is as following:
[{
  "Modell": "Latitude 9520",
  "Version": "A02",
  "Link": "https://ww w. dell. com/supp ort/kbdoc/000184698/latitude-9529-wind ows-10-driver-pack"
}]

and for empirumFunction its this:
{
  "Name": "Latitude E5450",
  "Windows7": null,
  "Windows10": "Dell_Latitude_E5450_A08.CAB",
  "WindowsServer2016": null,
  "WindowsServer2019": null
}




## Information on your n8n setup
- **n8n version:** 0.169.0
- **Database you're using (default: SQLite):** SQLite
- **Running n8n with the execution process [own(default), main]:** own
- **Running n8n via [Docker, npm, n8n.cloud, desktop app]:** truenas jail

Forgot to mention:
The Models in the output aren’t filtered at all.

Hi @gertz, welcome to the community :tada:

You could use a Merge node in “Keep Key Matches” mode for this.

Simply specific the name of the property from each input you want to filter on like so:

Afterwards, only the items present in both inputs should be passed on.

Example workflow:

If you need the full data from both sources, you can simply add another Merge node afterwards, this time in Merge By Key mode. No code required (my Function nodes simply mock some data here since I don’t have your source files):

Hope this helps!

2 Likes

Hello MutedJam.
Thank you for your input! That helped me a lot actually. :smiley:
Got my head wrapped around the idea to use JS… got it working in vscode already, but not with the function node. :sweat_smile:

1 Like

Sweet, I am glad to hear this helped, thanks a lot for confirming!

As for the JS part, I think the data structure could be the problem in your Function node. You are checking if dell.Modell == emp.Name, however, your items probably don’t have a Modell or Name property. Instead they’d have dell.json.Modell or emp.json.Name.

The json property is hidden in the UI and no longer required when returning data, but you can see the full data structure in your browser console if you do something like console.log(dell);.

I hope this makes sense, give me a shout if I am unclear here.

1 Like

Thank you so much! :slight_smile:
i’m literally learning javascript and json as i use n8n more. i learned programming at school, but thats already 10 years ago, and now i have to catch up. :smiley:

and i have to confess: its a lot of fun, to learn and make improvements. :slight_smile:

1 Like

@MutedJam when I copy and paste this workflow in my n8n, I get this:

Screenshot 2022-04-06 at 20.57.17

Hey @dickhoning, sorry to hear that. I just tried copying both of my example workflows from Trying to filter a json with another json - #3 by MutedJam again, but didn’t run into trouble here:

Wonder if this might have been any kind of temporary problem. Could you re-open your n8n window and try pasting this in a fresh workflow canvas?

1 Like

@MutedJam works fine when I copy it into a fresh workflow canvas, but not when I paste it into my (big/huge) data-transformation template/playground …