Loop through json

Hi, I get a json object from a webhook. I would look to loop though all blocks (see the image below), nb. of block could vary from 0 to10. I guess I should use SplitInBatches but despite having seen some example I don’t know how to do it… Is that node is the right to use or there something else to do ? thanks for help.

Hey @One1Tick,

Pop in an item lists node to split the data and for the input use body.block this will then split out the block away into items so you can use them easily.

Thanks @Jon for your help. I don’t think it is what I am looking for… unless I understand correctly what your explained.

I would like for all blocks execute some nodes, so in my current setup I have hard coded a maximum of 5 blocks.

For each block I test block[n][0] in the switch to create the corresponding http request to create a new block in notion. As you can see below I have hard coded 5 times the same nodes with block[n+1][0]. Instead of that I would like to loop through my blocks to be able to do i from 0 to n.

Sorry if I am not clear… Thanks for help.

Hey @One1Tick,

The split will give you n items so you wouldn’t need to use item[n] anymore as each node loops once per input item it should do what you are after.

If you can share your workflow and the example input data I can take more of a look but I think this will solve it.

Tried again wiht list item but noway. I should miss something.

Here is my simplified workflow and the json send to the webhook…
I would like with the nodes following the “items list” to be able to loop through all block (could have 1, 2, 3 to n blocks) and perform some actions.
Thanks for your time.

json :

  "name": "Name",
	"block" : [
        "Heading 1",
        "Test heading 1"
        "Test paragraph"

Tried again with this but noway to iterate through the json… can someone help please ? Thanks

Hey @One1Tick,

Using your recent example this would do the job.

But as it is split and all header values you can just use $json.header without the split in batches as we automatically loop items so you don’t need to in most nodes now.

Thanks a lot it works fine with your proposal, but…

1 - I dont understand why the If node should be “true” to continue looping as the test is “{{ $node[“Split In Batches2”].context[“noItemsLeft”] }}”, is n it a strange behavior ?

2 - I don’t see what you mean with " But as it is split and all header values you can just use $json.header without the split in batches as we automatically loop items so you don’t need to in most nodes now" ?

Thanks again for your appreciated help.

Hey @One1Tick,

With the way you had the If node running you were checking to see if No Items Left was false then going from the false branch. No Items left is going to be false while there is no items so your condition was true which can be seen in the If node when you run it.

With the Item looping this is what I mentioned before, Most n8n nodes will loop over all of the input items so if you have 4 items coming out of a node the next node will run 4 times once for each. In your example data you have 2 fields Header and Text so we can use {{ $json.Header }} and {{ $json.Text }} in the next node and we will use that item value for the node run so if we have the items that look like this…

On the next node run it will do it 4 times and produce the results below…

Run 1
$json.Header - Heading 1
$json.Text - Test heading 1

Run 2
$json.Header - Heading 2
$json.Text - Test heading 2

Run 3
$json.Header - Heading 3
$json.Text - Test heading 3

Run 4
$json.Header - Heading 4
$json.Text - Test heading 4

This means that your workflow example can be simplified and we can remove the Split in Batches and the If node so you just have the workflow below and you will get the same result.

If you have not already done it I would recommend going through the free courses which will teach you a bit more about how n8n works with data and maybe checkout the item looping docs.

Courses: https://docs.n8n.io/courses/
Item Looping: Looping | n8n Docs
Data Structure: Data structure | n8n Docs

1 Like

Thanks a lot @Jon very helpfull. I understand better the principle of loops with items. It was not clear before… I still have an issue with the Item List node but I will open a specific thread.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.