How to start a new function depending on the selected key?

For example, I send such data in JSON form:

{
  "item1" : "bonus1",
  "item2" : "bonus2"
}

in my other database that returns results in JSON form I have such results:

{
  "bonus1 and bonus2": "somethink i need",
  "bonusXYZ and bonus ABC" : "mby later"
}

And now I want to do something like that.
I’m checking the key value for item1:
// = bonus1
2. I’m looking for the value in the second JSON for this value so I am searching value for key including :
// bonus1 (but it could be a string of other characters, including, i.e bonus1withextratext)
3. if it exists, I return a new result to my new endpoint / trigger

I am honestly not sure if I understand you correctly. But here a possible solution:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "items[0].json = {\n  \"item1\" : \"bonus1\",\n  \"item2\" : \"bonus2\"\n};\nreturn items;"
      },
      "name": "Data 1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        400,
        600
      ]
    },
    {
      "parameters": {
        "functionCode": "items[0].json = {\n  \"bonus1 and bonus2\": \"somethink i need\",\n  \"bonusXYZ and bonus ABC\" : \"mby later\"\n};\nreturn items;"
      },
      "name": "Data 2",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        600,
        600
      ]
    },
    {
      "parameters": {
        "functionCode": "const itemsD1 = $node['Data 1'].json;\nconst keysD2 = Object.keys($node['Data 2'].json);\n\nconst matches = {};\nfor (const key1 of Object.keys(itemsD1)) {\n  matches[key1] = keysD2.filter(key2 => key2.includes(itemsD1[key1]));\n}\n\nitems[0].json = matches;\nreturn items;"
      },
      "name": "Find Matches",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        800,
        600
      ]
    }
  ],
  "connections": {
    "Data 1": {
      "main": [
        [
          {
            "node": "Data 2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Data 2": {
      "main": [
        [
          {
            "node": "Find Matches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

Thanks for your help. I understand the code, but now I don’t know if it’s a complete solution and I’m supposed to import it somehow or just part of the node ? if so, which one ?

You can copy the code and then paste it directly into n8n. It will then create 3 nodes. Two which create the mock data and the last one which contains the logic which you require.

Okay, I see you’re very helpful. I’m going to study, then. The first problem is in the beginning. I’m trying to activate the test node - webhook - google sheet.

I did as I wrote I copied and then added cred to api google (+id sheet and id sheet) . When I click on execute workflow it is still loading and then nothing happens

plus I don’t understand what the activate button is basically for. because when I click it and it gives an execute workflow, I get this error Screenshot from 2020-08-15 21-34-46

The solution for your first problem, that nothing happens, is in the lower right corner of the screenshot you did post. It says “Waiting for Webhook-Call”. Meaning it waits till you call the webhook with some data that it then can get used as initial data to start the workflow. In the most simple case if you have a webhook that listens to GET you can simply call the Test-Webhook-URL with your browser. Here the documentation of the Webhook-Node:
https://docs.n8n.io/nodes/n8n-nodes-base.webhook/#node-reference
Here a blogpost which uses the Webhook-Node:


And here also a tutorial video which uses it:

About activating a workflow. If a workflow gets activated the trigger nodes start to function (like listen for example for events or webhook calls under the production webhook-URL). If you have a workflow with a Webhook-Node which uses the path “/test” for the method GET and you activate it, this one is in use. If you then create another workflow with another Webhook-Node with exactly the same path and method and you also try to activate it you will see the error you did pst. Because if two identical ones would allowed to be activate at the same time it would be impossible for n8n to know which workflow should be started if it gets called. Exactly the same if two houses in the same street would have the same numbers.