Workflow with multiple lookup requests and IF nodes to check if data exist or need to be created

Hi,
I am trying to create a workflow where I get list of items (products list) that I use to check if they exist by send an HTTP request for each item which either sends back the complete item/product data or returns an empty array. If the response is an empty array, the next step is to check if the item/product group exists or needs to be created prior to create the item/product.
The problem I am facing is that I am not able to get the specific item details once I reach the IF Node. The HTTP request connected to the IF Node should pick that specific Item code/number or the group (category) name but when I try to use the previous HTTP Node output data (Item/Product) list, I get only the 1st item details. Appreciate if anyone can guide me through the possible scenarios, thanks in advance.

Hi @yabdali, sorry to hear you’re having trouble here.

What you describe would be the default behaviour in the expression editor. Say I have three items like this:

image

Now when connecting an IF node to the node outputting these three items, but the expression editor would only preview the first one:

image

However, when running,you can see it evaluates all three items:

So I am not sure I fully understand your problem. Would you be able to provide a simplified workflow including some static sample data showing the problem? Maybe it’s just an unexpected expression or something like that :slight_smile:

@MutedJam Thanks for your advice, here’s a sample. Basically, I have 4 items/products, 3 of them have the same group (Activities) while the 4th has a difference group (Adventures). The group (Adventures) doesn’t exist in the target system, the mocked data (Function-Groups) mocks that, the item for Adventures group is empty.

I want to be able differentiate which item group already exists and continue with the item creation without having to create a group, or if the group doesn’t exist, create the group and then create the item. Hope this explains my requirements clearly, thanks again.

{
  "name": "MWF",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        220,
        200
      ]
    },
    {
      "parameters": {
        "functionCode": "return [{json:\n[\n\t{\n\t\t\"item_title\": \"Kayaking\",\n\t\t\"item_price\": \"10\",\n\t\t\"item_sku\": \"CO-ACT-03\",\n\t\t\"item_type\": \"variable\",\n\t\t\"item_cat\": [\n\t\t\t\"Activities\"\n\t\t],\n\t\t\"item_var\": [\n\t\t\t{\n\t\t\t\t\"variation_id\": 735,\n\t\t\t\t\"variation_price\": \"10\",\n\t\t\t\t\"variation_sku\": \"CO-ACT-03-11\",\n\t\t\t\t\"variation_att\": [\n\t\t\t\t\t{\n\t\t\t\t\t\t\"att_name\": \"Kayak Type\",\n\t\t\t\t\t\t\"att_option\": \"single-kayak\",\n\t\t\t\t\t\t\"att_slug\": \"pa_kayak-type\"\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\t\"att_name\": \"Duration\",\n\t\t\t\t\t\t\"att_option\": \"1-hour\",\n\t\t\t\t\t\t\"att_slug\": \"pa_duration\"\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t},\n\t\t\t{\n\t\t\t\t\"variation_id\": 757,\n\t\t\t\t\"variation_price\": \"20\",\n\t\t\t\t\"variation_sku\": \"CO-ACT-03-12\",\n\t\t\t\t\"variation_att\": [\n\t\t\t\t\t{\n\t\t\t\t\t\t\"att_name\": \"Kayak Type\",\n\t\t\t\t\t\t\"att_option\": \"single-kayak\",\n\t\t\t\t\t\t\"att_slug\": \"pa_kayak-type\"\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\t\"att_name\": \"Duration\",\n\t\t\t\t\t\t\"att_option\": \"2-hours\",\n\t\t\t\t\t\t\"att_slug\": \"pa_duration\"\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}\n\t\t]\n\t},\n\t{\n\t\t\"item_title\": \"Banana Boat\",\n\t\t\"item_price\": \"35\",\n\t\t\"item_sku\": \"CO-ACT-02\",\n\t\t\"item_type\": \"variable\",\n\t\t\"item_cat\": [\n\t\t\t\"Activities\"\n\t\t],\n\t\t\"item_var\": [\n\t\t\t{\n\t\t\t\t\"variation_id\": 816,\n\t\t\t\t\"variation_price\": \"35\",\n\t\t\t\t\"variation_sku\": \"CO-ACT-02-01\",\n\t\t\t\t\"variation_att\": [\n\t\t\t\t\t{\n\t\t\t\t\t\t\"att_name\": \"Duration\",\n\t\t\t\t\t\t\"att_option\": \"1-hour\",\n\t\t\t\t\t\t\"att_slug\": \"pa_duration\"\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t},\n\t\t\t{\n\t\t\t\t\"variation_id\": 817,\n\t\t\t\t\"variation_price\": \"70\",\n\t\t\t\t\"variation_sku\": \"CO-ACT-02-02\",\n\t\t\t\t\"variation_att\": [\n\t\t\t\t\t{\n\t\t\t\t\t\t\"att_name\": \"Duration\",\n\t\t\t\t\t\t\"att_option\": \"2-hours\",\n\t\t\t\t\t\t\"att_slug\": \"pa_duration\"\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}\n\t\t]\n\t},\n\t{\n\t\t\"item_title\": \"Jet Skiing\",\n\t\t\"item_price\": \"25\",\n\t\t\"item_sku\": \"CO-ACT-01\",\n\t\t\"item_type\": \"variable\",\n\t\t\"item_cat\": [\n\t\t\t\"Activities\"\n\t\t],\n\t\t\"item_var\": [\n\t\t\t{\n\t\t\t\t\"variation_id\": 767,\n\t\t\t\t\"variation_price\": \"25\",\n\t\t\t\t\"variation_sku\": \"CO-ACT-01-01\",\n\t\t\t\t\"variation_att\": [\n\t\t\t\t\t{\n\t\t\t\t\t\t\"att_name\": \"Duration\",\n\t\t\t\t\t\t\"att_option\": \"1-hour\",\n\t\t\t\t\t\t\"att_slug\": \"pa_duration\"\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t},\n\t\t\t{\n\t\t\t\t\"variation_id\": 768,\n\t\t\t\t\"variation_price\": \"50\",\n\t\t\t\t\"variation_sku\": \"CO-ACT-01-02\",\n\t\t\t\t\"variation_att\": [\n\t\t\t\t\t{\n\t\t\t\t\t\t\"att_name\": \"Duration\",\n\t\t\t\t\t\t\"att_option\": \"2-hours\",\n\t\t\t\t\t\t\"att_slug\": \"pa_duration\"\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}\n\t\t]\n\t},\n\t{\n\t\t\"item_title\": \"Fun Diving\",\n\t\t\"item_price\": \"35\",\n\t\t\"item_sku\": \"CO-ADV-03\",\n\t\t\"item_type\": \"variable\",\n\t\t\"item_cat\": [\n\t\t\t\"Adventures\"\n\t\t],\n\t\t\"item_var\": [\n\t\t\t{\n\t\t\t\t\"variation_id\": 680,\n\t\t\t\t\"variation_price\": \"35\",\n\t\t\t\t\"variation_sku\": \"CO-TOR-04-01\",\n\t\t\t\t\"variation_att\": [\n\t\t\t\t\t{\n\t\t\t\t\t\t\"att_name\": \"Diving Gear\",\n\t\t\t\t\t\t\"att_option\": \"Excluded\",\n\t\t\t\t\t\t\"att_slug\": \"diving-gear\"\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t},\n\t\t\t{\n\t\t\t\t\"variation_id\": 681,\n\t\t\t\t\"variation_price\": \"50\",\n\t\t\t\t\"variation_sku\": \"CO-TOR-04-02\",\n\t\t\t\t\"variation_att\": [\n\t\t\t\t\t{\n\t\t\t\t\t\t\"att_name\": \"Diving Gear\",\n\t\t\t\t\t\t\"att_option\": \"Included\",\n\t\t\t\t\t\t\"att_slug\": \"diving-gear\"\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}\n\t\t]\n\t}\n]\n}\n]\n"
      },
      "name": "Function-Items",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        400,
        200
      ]
    },
    {
      "parameters": {
        "functionCode": "const results = []\nfor (const item of items[0].json) {\n      results.push({\n        json: item\n      })\n    \n}\nreturn results;\n\n"
      },
      "name": "Function-SplitItems",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        580,
        200
      ]
    },
    {
      "parameters": {
        "functionCode": "return [{json:\n[\n\t{\n\t\t\"data\": [\n\t\t\t{\n\t\t\t\t\"name\": \"Activities\",\n\t\t\t\t\"item_group_name\": \"Activities\",\n\t\t\t\t\"parent_item_group\": \"All Item Groups\",\n\t\t\t\t\"is_group\": 1,\n\t\t\t\t\"image\": null,\n\t\t\t\t\"show_in_website\": 0,\n\t\t\t\t\"route\": \"activities\",\n\t\t\t\t\"weightage\": 0,\n\t\t\t\t\"slideshow\": null,\n\t\t\t\t\"website_title\": \"Activities\",\n\t\t\t\t\"description\": null,\n\t\t\t\t\"lft\": 14,\n\t\t\t\t\"rgt\": 15,\n\t\t\t\t\"old_parent\": \"All Item Groups\",\n\t\t\t\t\"_user_tags\": null,\n\t\t\t\t\"_comments\": null,\n\t\t\t\t\"_assign\": null,\n\t\t\t\t\"_liked_by\": null\n\t\t\t}\n\t\t]\n\t},\n\t{\n\t\t\"data\": [\n\t\t\t{\n\t\t\t\t\"name\": \"Activities\",\n\t\t\t\t\"item_group_name\": \"Activities\",\n\t\t\t\t\"parent_item_group\": \"All Item Groups\",\n\t\t\t\t\"is_group\": 1,\n\t\t\t\t\"image\": null,\n\t\t\t\t\"show_in_website\": 0,\n\t\t\t\t\"route\": \"activities\",\n\t\t\t\t\"weightage\": 0,\n\t\t\t\t\"slideshow\": null,\n\t\t\t\t\"website_title\": \"Activities\",\n\t\t\t\t\"description\": null,\n\t\t\t\t\"lft\": 14,\n\t\t\t\t\"rgt\": 15,\n\t\t\t\t\"old_parent\": \"All Item Groups\",\n\t\t\t\t\"_user_tags\": null,\n\t\t\t\t\"_comments\": null,\n\t\t\t\t\"_assign\": null,\n\t\t\t\t\"_liked_by\": null\n\t\t\t}\n\t\t]\n\t},\n\t{\n\t\t\"data\": [\n\t\t\t{\n\t\t\t\t\"name\": \"Activities\",\n\t\t\t\t\"item_group_name\": \"Activities\",\n\t\t\t\t\"parent_item_group\": \"All Item Groups\",\n\t\t\t\t\"is_group\": 1,\n\t\t\t\t\"image\": null,\n\t\t\t\t\"show_in_website\": 0,\n\t\t\t\t\"route\": \"activities\",\n\t\t\t\t\"weightage\": 0,\n\t\t\t\t\"slideshow\": null,\n\t\t\t\t\"website_title\": \"Activities\",\n\t\t\t\t\"description\": null,\n\t\t\t\t\"lft\": 14,\n\t\t\t\t\"rgt\": 15,\n\t\t\t\t\"old_parent\": \"All Item Groups\",\n\t\t\t\t\"_user_tags\": null,\n\t\t\t\t\"_comments\": null,\n\t\t\t\t\"_assign\": null,\n\t\t\t\t\"_liked_by\": null\n\t\t\t}\n\t\t]\n\t},\n\t{\n\t\t\"data\": []\n\t}\n]\n}\n]\n"
      },
      "name": "Function-Groups",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        740,
        200
      ]
    },
    {
      "parameters": {
        "functionCode": "const results = []\nfor (const item of items[0].json) {\n      results.push({\n        json: item\n      })\n    \n}\nreturn results;\n\n"
      },
      "name": "Function-SplitGroups",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        900,
        200
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$node[\"Function-SplitGroups\"].json[\"data\"][0][\"item_group_name\"]}}",
              "value2": "={{$node[\"Function-SplitItems\"].json[\"item_cat\"][0]}}"
            },
            {
              "value2": "="
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1060,
        200
      ]
    },
    {
      "parameters": {
        "requestMethod": "POST",
        "url": "https://f89af380957a90fd53d8cd79c7dd0ac1.m.pipedream.net",
        "options": {},
        "bodyParametersUi": {
          "parameter": [
            {
              "name": "category_data",
              "value": "={{$node[\"IF\"].json[\"data\"][0][\"name\"]}}"
            }
          ]
        }
      },
      "name": "HTTP Req-IF-False",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        1280,
        360
      ]
    },
    {
      "parameters": {
        "requestMethod": "POST",
        "url": "https://f89af380957a90fd53d8cd79c7dd0ac1.m.pipedream.net",
        "options": {},
        "bodyParametersUi": {
          "parameter": [
            {
              "name": "category_data",
              "value": "={{$node[\"IF\"].json[\"data\"][0][\"item_group_name\"]}}"
            }
          ]
        }
      },
      "name": "HTTP Req-IF-True",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        1280,
        160
      ]
    }
  ],
  "connections": {
    "Function-Items": {
      "main": [
        [
          {
            "node": "Function-SplitItems",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function-Groups": {
      "main": [
        [
          {
            "node": "Function-SplitGroups",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start": {
      "main": [
        [
          {
            "node": "Function-Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function-SplitGroups": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function-SplitItems": {
      "main": [
        [
          {
            "node": "Function-Groups",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF": {
      "main": [
        [
          {
            "node": "HTTP Req-IF-True",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "HTTP Req-IF-False",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "id": 35
}

So in your use case, the data mocked by Function-SplitGroups & Function-Groups node from your example would come from a different source than the items mocked by Function-Items & Function-SplitItems?

And you want to perform an action for all possible values in the item_cat field for which no match is returned by the Function-SplitGroups node in the item_group_name field? That’s a bit tricky seeing one is an array and one is a string field inside an array of Objects. So you won’t be able to use the Merge node’s Remove Key Matches mode out of the box.

Assuming all the above is true and I understood you right, I’d use a Merge node in Pass-through mode to make sure we keep our data from Function-Items & Function-SplitItems before your IF node, then use a Function Item node to add a group containing the relevant info from Function-SplitGroups. You can then also use the group field in your IF node to check whether a group exists or not (and if not, create one based on the item data).

Example Workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [{json:\n[\n\t{\n\t\t\"item_title\": \"Kayaking\",\n\t\t\"item_price\": \"10\",\n\t\t\"item_sku\": \"CO-ACT-03\",\n\t\t\"item_type\": \"variable\",\n\t\t\"item_cat\": [\n\t\t\t\"Activities\"\n\t\t],\n\t\t\"item_var\": [\n\t\t\t{\n\t\t\t\t\"variation_id\": 735,\n\t\t\t\t\"variation_price\": \"10\",\n\t\t\t\t\"variation_sku\": \"CO-ACT-03-11\",\n\t\t\t\t\"variation_att\": [\n\t\t\t\t\t{\n\t\t\t\t\t\t\"att_name\": \"Kayak Type\",\n\t\t\t\t\t\t\"att_option\": \"single-kayak\",\n\t\t\t\t\t\t\"att_slug\": \"pa_kayak-type\"\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\t\"att_name\": \"Duration\",\n\t\t\t\t\t\t\"att_option\": \"1-hour\",\n\t\t\t\t\t\t\"att_slug\": \"pa_duration\"\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t},\n\t\t\t{\n\t\t\t\t\"variation_id\": 757,\n\t\t\t\t\"variation_price\": \"20\",\n\t\t\t\t\"variation_sku\": \"CO-ACT-03-12\",\n\t\t\t\t\"variation_att\": [\n\t\t\t\t\t{\n\t\t\t\t\t\t\"att_name\": \"Kayak Type\",\n\t\t\t\t\t\t\"att_option\": \"single-kayak\",\n\t\t\t\t\t\t\"att_slug\": \"pa_kayak-type\"\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\t\"att_name\": \"Duration\",\n\t\t\t\t\t\t\"att_option\": \"2-hours\",\n\t\t\t\t\t\t\"att_slug\": \"pa_duration\"\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}\n\t\t]\n\t},\n\t{\n\t\t\"item_title\": \"Banana Boat\",\n\t\t\"item_price\": \"35\",\n\t\t\"item_sku\": \"CO-ACT-02\",\n\t\t\"item_type\": \"variable\",\n\t\t\"item_cat\": [\n\t\t\t\"Activities\"\n\t\t],\n\t\t\"item_var\": [\n\t\t\t{\n\t\t\t\t\"variation_id\": 816,\n\t\t\t\t\"variation_price\": \"35\",\n\t\t\t\t\"variation_sku\": \"CO-ACT-02-01\",\n\t\t\t\t\"variation_att\": [\n\t\t\t\t\t{\n\t\t\t\t\t\t\"att_name\": \"Duration\",\n\t\t\t\t\t\t\"att_option\": \"1-hour\",\n\t\t\t\t\t\t\"att_slug\": \"pa_duration\"\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t},\n\t\t\t{\n\t\t\t\t\"variation_id\": 817,\n\t\t\t\t\"variation_price\": \"70\",\n\t\t\t\t\"variation_sku\": \"CO-ACT-02-02\",\n\t\t\t\t\"variation_att\": [\n\t\t\t\t\t{\n\t\t\t\t\t\t\"att_name\": \"Duration\",\n\t\t\t\t\t\t\"att_option\": \"2-hours\",\n\t\t\t\t\t\t\"att_slug\": \"pa_duration\"\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}\n\t\t]\n\t},\n\t{\n\t\t\"item_title\": \"Jet Skiing\",\n\t\t\"item_price\": \"25\",\n\t\t\"item_sku\": \"CO-ACT-01\",\n\t\t\"item_type\": \"variable\",\n\t\t\"item_cat\": [\n\t\t\t\"Activities\"\n\t\t],\n\t\t\"item_var\": [\n\t\t\t{\n\t\t\t\t\"variation_id\": 767,\n\t\t\t\t\"variation_price\": \"25\",\n\t\t\t\t\"variation_sku\": \"CO-ACT-01-01\",\n\t\t\t\t\"variation_att\": [\n\t\t\t\t\t{\n\t\t\t\t\t\t\"att_name\": \"Duration\",\n\t\t\t\t\t\t\"att_option\": \"1-hour\",\n\t\t\t\t\t\t\"att_slug\": \"pa_duration\"\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t},\n\t\t\t{\n\t\t\t\t\"variation_id\": 768,\n\t\t\t\t\"variation_price\": \"50\",\n\t\t\t\t\"variation_sku\": \"CO-ACT-01-02\",\n\t\t\t\t\"variation_att\": [\n\t\t\t\t\t{\n\t\t\t\t\t\t\"att_name\": \"Duration\",\n\t\t\t\t\t\t\"att_option\": \"2-hours\",\n\t\t\t\t\t\t\"att_slug\": \"pa_duration\"\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}\n\t\t]\n\t},\n\t{\n\t\t\"item_title\": \"Fun Diving\",\n\t\t\"item_price\": \"35\",\n\t\t\"item_sku\": \"CO-ADV-03\",\n\t\t\"item_type\": \"variable\",\n\t\t\"item_cat\": [\n\t\t\t\"Adventures\"\n\t\t],\n\t\t\"item_var\": [\n\t\t\t{\n\t\t\t\t\"variation_id\": 680,\n\t\t\t\t\"variation_price\": \"35\",\n\t\t\t\t\"variation_sku\": \"CO-TOR-04-01\",\n\t\t\t\t\"variation_att\": [\n\t\t\t\t\t{\n\t\t\t\t\t\t\"att_name\": \"Diving Gear\",\n\t\t\t\t\t\t\"att_option\": \"Excluded\",\n\t\t\t\t\t\t\"att_slug\": \"diving-gear\"\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t},\n\t\t\t{\n\t\t\t\t\"variation_id\": 681,\n\t\t\t\t\"variation_price\": \"50\",\n\t\t\t\t\"variation_sku\": \"CO-TOR-04-02\",\n\t\t\t\t\"variation_att\": [\n\t\t\t\t\t{\n\t\t\t\t\t\t\"att_name\": \"Diving Gear\",\n\t\t\t\t\t\t\"att_option\": \"Included\",\n\t\t\t\t\t\t\"att_slug\": \"diving-gear\"\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}\n\t\t]\n\t}\n]\n}\n]\n"
      },
      "name": "Function-Items",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const results = []\nfor (const item of items[0].json) {\n      results.push({\n        json: item\n      })\n    \n}\nreturn results;\n\n"
      },
      "name": "Function-SplitItems",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [{json:\n[\n\t{\n\t\t\"data\": [\n\t\t\t{\n\t\t\t\t\"name\": \"Activities\",\n\t\t\t\t\"item_group_name\": \"Activities\",\n\t\t\t\t\"parent_item_group\": \"All Item Groups\",\n\t\t\t\t\"is_group\": 1,\n\t\t\t\t\"image\": null,\n\t\t\t\t\"show_in_website\": 0,\n\t\t\t\t\"route\": \"activities\",\n\t\t\t\t\"weightage\": 0,\n\t\t\t\t\"slideshow\": null,\n\t\t\t\t\"website_title\": \"Activities\",\n\t\t\t\t\"description\": null,\n\t\t\t\t\"lft\": 14,\n\t\t\t\t\"rgt\": 15,\n\t\t\t\t\"old_parent\": \"All Item Groups\",\n\t\t\t\t\"_user_tags\": null,\n\t\t\t\t\"_comments\": null,\n\t\t\t\t\"_assign\": null,\n\t\t\t\t\"_liked_by\": null\n\t\t\t}\n\t\t]\n\t},\n\t{\n\t\t\"data\": [\n\t\t\t{\n\t\t\t\t\"name\": \"Activities\",\n\t\t\t\t\"item_group_name\": \"Activities\",\n\t\t\t\t\"parent_item_group\": \"All Item Groups\",\n\t\t\t\t\"is_group\": 1,\n\t\t\t\t\"image\": null,\n\t\t\t\t\"show_in_website\": 0,\n\t\t\t\t\"route\": \"activities\",\n\t\t\t\t\"weightage\": 0,\n\t\t\t\t\"slideshow\": null,\n\t\t\t\t\"website_title\": \"Activities\",\n\t\t\t\t\"description\": null,\n\t\t\t\t\"lft\": 14,\n\t\t\t\t\"rgt\": 15,\n\t\t\t\t\"old_parent\": \"All Item Groups\",\n\t\t\t\t\"_user_tags\": null,\n\t\t\t\t\"_comments\": null,\n\t\t\t\t\"_assign\": null,\n\t\t\t\t\"_liked_by\": null\n\t\t\t}\n\t\t]\n\t},\n\t{\n\t\t\"data\": [\n\t\t\t{\n\t\t\t\t\"name\": \"Activities\",\n\t\t\t\t\"item_group_name\": \"Activities\",\n\t\t\t\t\"parent_item_group\": \"All Item Groups\",\n\t\t\t\t\"is_group\": 1,\n\t\t\t\t\"image\": null,\n\t\t\t\t\"show_in_website\": 0,\n\t\t\t\t\"route\": \"activities\",\n\t\t\t\t\"weightage\": 0,\n\t\t\t\t\"slideshow\": null,\n\t\t\t\t\"website_title\": \"Activities\",\n\t\t\t\t\"description\": null,\n\t\t\t\t\"lft\": 14,\n\t\t\t\t\"rgt\": 15,\n\t\t\t\t\"old_parent\": \"All Item Groups\",\n\t\t\t\t\"_user_tags\": null,\n\t\t\t\t\"_comments\": null,\n\t\t\t\t\"_assign\": null,\n\t\t\t\t\"_liked_by\": null\n\t\t\t}\n\t\t]\n\t},\n\t{\n\t\t\"data\": []\n\t}\n]\n}\n]\n"
      },
      "name": "Function-Groups",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        900,
        480
      ]
    },
    {
      "parameters": {
        "functionCode": "const results = []\nfor (const item of items[0].json) {\n      results.push({\n        json: item\n      })\n    \n}\nreturn results;\n\n"
      },
      "name": "Function-SplitGroups",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1120,
        480
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [],
          "boolean": [],
          "number": [
            {
              "value1": "={{$json[\"group\"][\"is_group\"]}}",
              "operation": "larger"
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1780,
        320
      ]
    },
    {
      "parameters": {
        "requestMethod": "POST",
        "url": "https://f89af380957a90fd53d8cd79c7dd0ac1.m.pipedream.net",
        "options": {},
        "bodyParametersUi": {
          "parameter": [
            {
              "name": "category_data",
              "value": "={{$node[\"IF\"].json[\"data\"][0][\"name\"]}}"
            }
          ]
        }
      },
      "name": "HTTP Req-IF-False",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        2000,
        420
      ],
      "disabled": true
    },
    {
      "parameters": {
        "requestMethod": "POST",
        "url": "https://f89af380957a90fd53d8cd79c7dd0ac1.m.pipedream.net",
        "options": {},
        "bodyParametersUi": {
          "parameter": [
            {
              "name": "category_data",
              "value": "={{$node[\"IF\"].json[\"data\"][0][\"item_group_name\"]}}"
            }
          ]
        }
      },
      "name": "HTTP Req-IF-True",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        2000,
        220
      ],
      "disabled": true
    },
    {
      "parameters": {
        "mode": "passThrough"
      },
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        1340,
        320
      ]
    },
    {
      "parameters": {
        "functionCode": "let groups = [];\n$items(\"Function-SplitGroups\").map(e => e.json.data).forEach(e => {\n  groups = groups.concat(e);\n});\nitem.group = groups.find(e => e.name == item.item_cat[0]);\nreturn item;"
      },
      "name": "Assign Group",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        1560,
        320
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function-Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function-Items": {
      "main": [
        [
          {
            "node": "Function-SplitItems",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function-SplitItems": {
      "main": [
        [
          {
            "node": "Function-Groups",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function-Groups": {
      "main": [
        [
          {
            "node": "Function-SplitGroups",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function-SplitGroups": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "IF": {
      "main": [
        [
          {
            "node": "HTTP Req-IF-True",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "HTTP Req-IF-False",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Assign Group",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Assign Group": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

In this example, you’ll have a group field after the Function Item node (called “Assign Group” here):

You can see how this field is empty for the Fun Diving item because no Adventure group exists yet. This is checked in the IF node which checks whether the value of group.is_group is larger than 0. This test passes for the first three items but fails for Fun Diving which is sent to the false output of the IF node.

You can then make the API request creating the Adventure (or any other group, provided you are using a suitable expression) as needed (and afterwards proceed for all items on the same path again).

Hope this helps, let me know if you run into any trouble here :slight_smile:

1 Like

@MutedJam I can’t express how wonderful being in this community, you and the team make a difference compared to other solutions when it comes to community support.
Thanks for the insights, this is definitely something that would make it easier to add data to the target system without very complex workflows. To be honest, I had already tried a different approach where I send the data using HTTP requests and ignore the error messages which’s not the optimal way and also means that I keep generating uncessary load on the target server.
Again, thanks a lot and I will be following your suggestion in my workflow.

2 Likes

Thanks so much for your kind words, it’s great hearing that :slight_smile: