Repeat items

Ah sorry. No all fine in n8n. Did just not nsee that you were making 2 requests.

The first request worked fine as the node “jsondemo”, from where are referencing the data, has also one item. But on the second one it failed, as “jsondemo” only contains one. So all that had to be done is change the expression on the HTTP-Request node in “Body Parameters” to always take the first item of “jsondemo” for all requests.

Whole workflow with changed expression:

// Before:
{{$node["jsondemo"].json}}
// Now:
{{$item(0).$node["jsondemo"].json}}
{
  "nodes": [
    {
      "parameters": {
        "functionCode": "const data = [\n{\n\"ID\": \"1001\",\n\"Title\": \"Event1\",\n\"Title_event\": \"Checklist\",\n\"url\": \"https://n8n.remotelocation.dev/webhook/89/webhook/rec7Bznsz22hICBrn\"\n},\n{\n\"ID\": \"1002\",\n\"Title\": \"Event1\",\n\"Title_event\": \"Checklist2\",\n\"url\": \"https://n8n.remotelocation.dev/webhook/89/webhook/rec7Bznsz22hICBrn\"\n},\n{\n\"ID\": \"1003\",\n\"Title\": \"Event2\",\n\"Title_event\": \"Checklist3\",\n\"url\": \"https://n8n.remotelocation.dev/webhook/89/webhook/rec7Bznsz22hICBrn\"\n}\n];\n\nconst title = $node[\"jsondemo\"].json[\"eventData\"][\"title\"];\n\nconst items = data.filter(entry => {\n    let isValid = true;\n    isValid = entry.Title === title ? isValid : false;\n    return isValid;\n});\n\nconst newItems = [];\nitems.forEach(item => newItems.push({\n    json: item\n}));\nreturn newItems;\n"
      },
      "name": "Triggers1",
      "type": "n8n-nodes-base.function",
      "position": [
        650,
        300
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "requestMethod": "POST",
        "url": "={{$node[\"Triggers1\"].json[\"url\"]}}",
        "jsonParameters": true,
        "options": {},
        "bodyParametersJson": "={{$item(0).$node[\"jsondemo\"].json}}"
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n{\n    json: {\n\"eventData\":{\n\"title\": \"Event1\",\n\"data1\": \"test1\",\n\"data2\": \"test2\"\n}\n}\n  }\n]"
      },
      "name": "jsondemo",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    }
  ],
  "connections": {
    "Triggers1": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "jsondemo": {
      "main": [
        [
          {
            "node": "Triggers1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Thank you, worked very well.

Kind regards
Mattias

Just got 1 little issue

How do i get only the body instead of the whole Json i have the trouble to get that i get body/body instead of only body.

i tried $item(0).$node[“Webhook”].[“body”].json

The first picture show double and I would like to get second value (I left both just to show)

I only would like one body in the racing event.

Screenshot 2020-04-20 at 12.28.02

Kind regards
Mattias

Would be the same. You simply put $item(0) in front of it:

{{$item(0).$node["jsondemo"].json["body"]}}

Related to this subject, I’ve developed an in-house node to simplify a bit the data manipulation thing…
Here’s a screenshot with it:

The node can do two things:

  1. extract items from an object that contains an array (check the below example)
    For this incoming data:
[
  {
    "data": {
      "items": [
        {
          "incoming1": "v1",
          "incoming2": "v2"
        },
        {
          "incoming1": "v1_2",
          "incoming2": "v2_2"
        }
      ]
    }
  },
  {
    "data": {
      "items": [
        {
          "incoming1": "v1__2",
          "incoming2": "v2__2"
        },
        {
          "incoming1": "v1_2__2",
          "incoming2": "v2_2__2"
        }
      ]
    }
  }
]

the node will output:

[
  {
    "incoming1": "v1",
    "incoming2": "v2"
  },
  {
    "incoming1": "v1_2",
    "incoming2": "v2_2"
  },
  {
    "incoming1": "v1__2",
    "incoming2": "v2__2"
  },
  {
    "incoming1": "v1_2__2",
    "incoming2": "v2_2__2"
  }
]
  1. the node will define the output format of each object (check the below example)
    From this data:
[
  {
    "incoming1": "v1",
    "incoming2": "v2"
  },
  {
    "incoming1": "v1_2",
    "incoming2": "v2_2"
  },
  {
    "incoming1": "v1__2",
    "incoming2": "v2__2"
  },
  {
    "incoming1": "v1_2__2",
    "incoming2": "v2_2__2"
  }
]

You will get

[
  {
    "outcoming1": "v1",
    "outcoming2": "v2"
  },
  {
    "outcoming1": "v1_2",
    "outcoming2": "v2_2"
  },
  {
    "outcoming1": "v1__2",
    "outcoming2": "v2__2"
  },
  {
    "outcoming1": "v1_2__2",
    "outcoming2": "v2_2__2"
  }
]

I’m pretty busy in this period of time to make this node open-source, but if someone of you need it, I’ll prioritise this.

2 Likes