Skip to content

n8n 如何组织数据(How n8n structures data)#

理解 n8n 如何在节点之间构建和传递数据对于构建工作流至关重要。本指南涵盖了数据结构格式以及数据如何在工作流中流动。

🌐 Understanding how n8n structures and passes data between nodes is fundamental to building workflows. This guide covers both the data structure format and how data flows through your workflow.

数据结构(Data structure)#

在 n8n 中,节点之间传递的所有数据都是对象数组。其结构如下:

🌐 In n8n, all data passed between nodes is an array of objects. It has the following structure:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[
	{
		// For most data:
		// Wrap each item in another object, with the key 'json'
		"json": {
			// Example data
			"apple": "beets",
			"carrot": {
				"dill": 1
			}
		},
		// For binary data:
		// Wrap each item in another object, with the key 'binary'
		"binary": {
			// Example data
			"apple-picture": {
				"data": "....", // Base64 encoded binary data (required)
				"mimeType": "image/png", // Best practice to set if possible (optional)
				"fileExtension": "png", // Best practice to set if possible (optional)
				"fileName": "example.png", // Best practice to set if possible (optional)
			}
		}
	},
]

跳过 json 键和数组语法

从 0.166.0 版本开始,在使用 Function 节点或 Code 节点时,如果缺少 json 键,n8n 会自动添加它。如果需要,它还会自动将你的项目封装在一个数组([])中。只有在使用 Function 或 Code 节点时才会这样。当你构建自己的节点时,仍然必须确保节点返回的数据包含 json 键。

数据在节点内的流动(How data flows within nodes)#

当你在工作流中连接节点时,数据会自动从一个节点传递到下一个节点。

🌐 When you connect nodes in a workflow, data automatically passes from one node to the next.

节点会自动处理多个项目。当节点接收到一组数据项时,它会单独处理每个项目,并对每个项目执行配置的操作。

🌐 Nodes process multiple items automatically. When a node receives an array of data items, it processes each item individually and performs the configured operation for each one.

例如,如果你将 Trello 节点设置为 Create-Card,并创建一个表达式来使用传入数据中的名为 name-input-value 的属性设置 Name,节点会为每个项目创建一张卡片,总是选择当前项目的 name-input-value

🌐 For example, if you set the Trello node to Create-Card, and create an expression that sets Name using a property called name-input-value from the incoming data, the node creates a card for each item, always choosing the name-input-value of the current item.

例如,这个输入将创建两张卡片。一张命名为 test1,另一张命名为 test2

🌐 For example, this input will create two cards. One named test1 the other one named test2:

1
2
3
4
5
6
7
8
[
	{
		"name-input-value": "test1"
	},
	{
		"name-input-value": "test2"
	}
]

理解拖放映射的内容删除(Understand what you're mapping with drag and drop)#

数据映射映射字段路径,并加载字段的值。例如,给定以下数据:

🌐 Data mapping maps the field path, and loads the field's value. For example, given the following data:

1
2
3
4
5
6
[
	{
		"fruit": "apples",
		"color": "green"
	}
]

你可以通过将 INPUT 中的 水果 拖放到你想使用其值的字段中来映射 fruit。这会创建一个表达式 {{ $json.fruit }}。当节点迭代输入项时,该字段的值将变为每个项的 fruit 值。

🌐 You can map fruit by dragging and dropping fruit from the INPUT into the field where you want to use its value. This creates an expression, {{ $json.fruit }}. When the node iterates over input items, the value of the field becomes the value of fruit for each item.

理解嵌套数据(Understand nested data)#

给定以下数据:

🌐 Given the following data:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
[
  {
    "name": "First item",
    "nested": {
      "example-number-field": 1,
      "example-string-field": "apples"
    }
  },
  {
    "name": "Second item",
    "nested": {
      "example-number-field": 2,
      "example-string-field": "oranges"
    }
  }
]

n8n 以表格形式显示如下内容:

🌐 n8n displays it in table form like this:

"Screenshot of a table in the INPUT panel. It includes a top level field named "nested." This field contains nested data, which is indicated in bold."