Intermediate
How to use looping for data processing on n8n
Quick Answer
Looping in n8n allows you to process multiple data items iteratively using the Loop Over Items node or Split In Batches node. You can create loops to handle arrays, perform batch processing, and apply transformations to each data item individually.
Prerequisites
- Basic understanding of n8n workflows
- Familiarity with JSON data structures
- Knowledge of HTTP requests and API endpoints
- Understanding of data transformation concepts
1
Add a Loop Over Items node to your workflow
In your n8n workflow canvas, click the + button to add a new node. Search for Loop Over Items in the node library and select it. This node will process each item in your input data array individually through the loop.
Tip
Use Loop Over Items when you need to process each array item with the same set of operations.
2
Configure the Loop Over Items node settings
Connect your data source node to the Loop Over Items node. In the node parameters, set the Field Name to specify which field contains the array you want to loop through. If your entire input is an array, leave this field empty. Configure the Batch Size if you want to process multiple items at once.
Tip
Set a reasonable batch size to avoid memory issues with large datasets.
3
Add processing nodes inside the loop
Connect processing nodes after the Loop Over Items node. These can include HTTP Request, Code, Set, or any other nodes that will operate on each individual item. Each node will receive one item at a time from the loop iteration.
4
Configure data transformation within the loop
In your processing nodes, use expressions like
{{ $json.fieldName }} to access data from the current loop item. Add a Set node to transform or restructure the data. Configure field mappings using {{ $json.originalField }} to reference the current item's properties.Tip
Use the Expression Editor to test your data transformations before running the full loop.
5
Handle loop completion and data aggregation
After your processing nodes, the data automatically flows back to continue the loop. Once all items are processed, connect a node after the Loop Over Items to handle the final aggregated results. Use nodes like Merge or Code to combine all processed items.
Tip
The Loop Over Items node automatically collects all processed items into a single output array.
6
Implement error handling within loops
Add an Error Trigger node or use the Continue on Fail option in processing nodes to handle errors gracefully. Configure the If node to add conditional logic within your loop. Set up proper error logging using the Set node to capture failed items.
Tip
Always implement error handling to prevent the entire loop from failing due to one problematic item.
7
Test and optimize loop performance
Execute your workflow with a small dataset first to verify the loop logic. Monitor the Execution panel to check processing time and memory usage. For large datasets, consider using Split In Batches instead of Loop Over Items for better performance. Adjust batch sizes based on your system capabilities.
Tip
Use the workflow execution history to identify bottlenecks and optimize your loop structure.
Troubleshooting
Loop processes the entire array as one item instead of individual items
Check that your input data is properly formatted as an array. Verify the Field Name setting in the Loop Over Items node points to the correct array field, or leave it empty if the entire input is an array.
Workflow times out or runs out of memory during loop execution
Reduce the batch size in your loop configuration. Consider using Split In Batches node instead of Loop Over Items for large datasets. Add Wait nodes between processing steps to prevent overwhelming external APIs.
Data from previous loop iterations is overwritten or lost
Ensure you're using proper data aggregation after the loop. Check that processing nodes inside the loop are correctly configured to preserve data. Use the Merge node or custom code to properly combine results from all iterations.
Loop stops executing after encountering an error in one iteration
Enable Continue on Fail in the processing nodes within your loop. Add proper error handling using If nodes to catch and handle exceptions. Implement error logging to track which items failed during processing.
Ready to get started with n8n?
Put this tutorial into practice. Visit n8n and follow the steps above.
Visit n8n →