Connecting Nodes (JSON)


Just our of curiosity, I was going through the JSON File generated after we download the workflow. I would like to understand these two keys (main & index) that are in the JSON Object when we connect lets say Node A to Node B

"connections": {
		"main":[     #This Key
						"node": "B",
						"type": "main",
						"index": 0      #This Key



That comes from the early n8n days. I wanted to make sure that I keep the design flexible enough that I could easily extend it in the future (obviously also resulted in currently much more complicated code than it would have to be). So not just the connections that exist right now, that decide which nodes get executed after another. Those connections that currently exist in n8n are that main ones.

The idea was/is to also add in the future additional types for other functionality like error-reporting, filtering, configuration, … So you could then have additional inputs & outputs on nodes for that functionality. Only if the input & output are of the same type is it possible to connect them.

You can also see that in the nodes code. Here for example in the IF-Node:

There you can see that it has one main input and two main outputs.

The index describes the index of the input the node is connected with. The most nodes have only one input but some like the Merge-Node have multiple ones and if that is set to 1 instead of 0 n8n knows that it is not connected to the first, rather to the second one.
Btw. the same is true for outputs. As some nodes can have multiple outputs (like the above mentioned If-Node) is the reverse of that index the location of the data in the array underneath main. The connections in the first array underneath main are for output 0 and the ones in index 1, 2, … are for the other outputs.

Hope that makes that clear.

Thanks @jan for explaining the concept. :smiley:

Sure, happy if it helped!