了解数据结构#
¥Understanding the data structure
本章将介绍 n8n 的数据结构,以及如何使用 代码节点 转换数据并模拟节点输出。
¥In this chapter, you will learn about the data structure of n8n and how to use the Code node to transform data and simulate node outputs.
n8n 的数据结构#
¥Data structure of n8n
从本质上讲,n8n 节点的功能类似于提取、转换和加载 (ETL) 工具。这些节点允许你从多个不同的数据源访问(提取)数据,以特定方式修改(转换)数据,并将其传递(加载)到所需位置。
¥In a basic sense, n8n nodes function as an Extract, Transform, Load (ETL) tool. The nodes allow you to access (extract) data from multiple disparate sources, modify (transform) that data in a particular way, and pass (load) it along to where it needs to be.
工作流中节点间传递的数据必须采用每个节点都能识别和解释的格式(结构)。在 n8n 中,此必需结构是一个对象数组。
¥The data that moves along from node to node in your workflow must be in a format (structure) that can be recognized and interpreted by each node. In n8n, this required structure is an array of objects.
About array of objects
数组是值的列表。数组可以为空,也可以包含多个元素。每个元素都存储在列表中的一个位置(索引),从 0 开始,可以通过索引号引用。例如,在数组 ["Leonardo", "Michelangelo", "Donatello", "Raphael"]; 中,元素 Donatello 存储在索引 2 处。
¥An array is a list of values. The array can be empty or contain several elements. Each element is stored at a position (index) in the list, starting at 0, and can be referenced by the index number. For example, in the array ["Leonardo", "Michelangelo", "Donatello", "Raphael"]; the element Donatello is stored at index 2.
对象存储的是键值对,而不是像数组那样存储带有编号索引的值。键值对的顺序并不重要,因为可以通过引用键名来访问值。例如,以下对象包含两个属性(name 和 color):
¥An object stores key-value pairs, instead of values at numbered indexes as in arrays. The order of the pairs isn't important, as the values can be accessed by referencing the key name. For example, the object below contains two properties (name and color):
1 2 3 4 | |
对象数组是包含一个或多个对象的数组。例如,下面的数组 turtles 包含四个对象:
¥An array of objects is an array that contains one or more objects. For example, the array turtles below contains four objects:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
你可以使用点号表示法,通过语法 object.property 访问对象的属性。例如,turtles[1].color 获取第二个海龟的颜色。
¥You can access the properties of an object using dot notation with the syntax object.property. For example, turtles[1].color gets the color of the second turtle.
从一个节点发送到另一个节点的数据以 JSON 对象数组的形式发送。此集合中的元素称为项目。
¥Data sent from one node to another is sent as an array of JSON objects. The elements in this collection are called items.

n8n 节点对传入的每个数据项执行操作。
¥An n8n node performs its action on each item of incoming data.

使用代码节点创建数据集#
¥Creating data sets with the Code node
既然你已经熟悉了 n8n 数据结构,就可以使用它来创建自己的数据集或模拟节点输出。要实现此操作,请使用 代码节点 编写 JavaScript 代码,定义具有以下结构的对象数组:
¥Now that you are familiar with the n8n data structure, you can use it to create your own data sets or simulate node outputs. To do this, use the Code node to write JavaScript code defining your array of objects with the following structure:
1 2 3 4 5 6 7 | |
例如,表示忍者神龟的对象数组在代码节点中如下所示:
¥For example, the array of objects representing the Ninja turtles would look like this in the Code node:

JSON objects
请注意,此对象数组包含一个额外的键:json。n8n 要求你将数组中的每个对象封装在另一个对象中,并以 json 为键。
¥Notice that this array of objects contains an extra key: json. n8n expects you to wrap each object in an array in another object, with the key json.

最佳实践是以 n8n 使用的正确结构传递数据。但如果你忘记向某个条目添加 json 键,不用担心,n8n(0.166.0 及以上版本)会自动添加。
¥It's good practice to pass the data in the right structure used by n8n. But don't worry if you forget to add the json key to an item, n8n (version 0.166.0 and above) adds it automatically.
你还可以使用嵌套的键值对,例如,如果你想定义主色和辅助色。在这种情况下,你需要将键值对进一步用大括号 {} 封装起来。
¥You can also have nested pairs, for example if you want to define a primary and a secondary color. In this case, you need to further wrap the key-value pairs in curly braces {}.
n8n data structure video
本次演讲 提供对 n8n 数据结构的更详细解释。
¥This talk offers a more detailed explanation of data structure in n8n.
练习#
¥Exercise
在代码节点中,创建一个名为 myContacts 的对象数组,其中包含属性 name 和 email,并将属性 email 进一步拆分为 personal 和 work。
¥In a Code node, create an array of objects named myContacts that contains the properties name and email, and the email property is further split into personal and work.
显示解决方案
¥??? note "Show me the solution"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
使用代码节点引用节点数据#
¥Referencing node data with the Code node
就像你可以使用 expressions 引用其他节点的数据一样,你也可以在“代码”节点中使用一些 方法和变量。
¥Just like you can use expressions to reference data from other nodes, you can also use some methods and variables in the Code node.
请务必在继续下一个练习之前阅读这些页面。
¥Please make sure you read these pages before continuing to the next exercise.
练习#
¥Exercise
让我们在上一个练习的基础上继续,在上一个练习中,你使用代码节点创建了一个包含两个联系人名称和电子邮件地址的数据集。现在,将第二个代码节点连接到第一个代码节点。在新节点中,编写代码创建一个名为 workEmail 的新列,该列引用第一个联系人的工作邮箱。
¥Let's build on the previous exercise, in which you used the Code node to create a data set of two contacts with their names and emails. Now, connect a second Code node to the first one. In the new node, write code to create a new column named workEmail that references the work email of the first contact.
???注意 "显示解决方案":在“代码”节点的“JavaScript 代码”字段中,你需要编写以下代码:
¥??? note "Show me the solution" In the Code node, in the JavaScript Code field you have to write the following code:
1 2 3 4 5 6 7 8 9 | |
转换数据#
¥Transforming data
某些节点传入的数据结构可能与 n8n 中使用的数据结构不同。在这种情况下,你需要使用 转换数据,以便可以单独处理每个项目。
¥The incoming data from some nodes may have a different data structure than the one used in n8n. In this case, you need to transform the data, so that each item can be processed individually.
数据转换的两个最常用操作是:
¥The two most common operations for data transformation are:
- 从单个项目创建多个项目
¥Creating multiple items from one item
- 从多个项目创建单个项目
¥Creating a single item from multiple items
有几种方法可以转换数据以实现上述目的:
¥There are several ways to transform data for the purposes mentioned above:
- 使用 n8n 的 数据转换节点。使用这些节点来修改包含列表(数组)的传入数据的结构,而无需在“代码”节点中使用 JavaScript 代码:
¥Use n8n's data transformation nodes. Use these nodes to modify the structure of incoming data that contain lists (arrays) without needing to use JavaScript code in the Code node:
-
使用 Split Out 节点 将包含列表的单个数据项拆分为多个项。
¥Use the Split Out node to separate a single data item containing a list into multiple items.
-
使用 聚合节点 将单个项目或其部分组合成单独的项目。
¥Use the Aggregate node to take separate items, or portions of them, and group them together into individual items.
-
使用代码节点编写 JavaScript 函数,以使用“一次运行,所有项目”模式修改传入数据的数据结构。
¥Use the Code node to write JavaScript functions to modify the data structure of incoming data using the Run Once for All Items mode:
-
要从单个项目创建多个项目,你可以使用如下 JavaScript 代码。此示例假设该项具有名为
data的键,该键设置为如下形式的项数组:[{ "data": [{<item_1>}, {<item_2>}, ...] }]:¥To create multiple items from a single item, you can use JavaScript code like this. This example assumes that the item has a key named
dataset to an array of items in the form of:[{ "data": [{<item_1>}, {<item_2>}, ...] }]:1 2 3 4 5
return $input.first().json.data.map(item => { return { json: item } }); -
要从多个项目创建单个项目,你可以使用以下 JavaScript 代码:
¥To create a single item from multiple items, you can use this JavaScript code:
1 2 3 4 5 6 7
return [ { json: { data_object: $input.all().map(item => item.json) } } ];
这些 JavaScript 示例假设你要转换的所有内容都是你的输入。如上例所示,你也可以通过在项目列表中指定字段来对特定字段执行操作。例如,如果我们的 workEmail 示例中的单个字段包含多个电子邮件地址,我们可以运行如下代码:
¥These JavaScript examples assume your entire input is what you want to transform. As in the exercise above, you can also execute either operation on a specific field by identifying that in the items list, for example, if our workEmail example had multiple emails in a single field, we could run some code like this:
1 2 3 4 5 6 | |
练习#
¥Exercise
- 使用 HTTP 请求节点向 PokéAPI
https://pokeapi.co/api/v2/pokemon发送 GET 请求。(此 API 无需身份验证。)
¥Use the HTTP Request node to make a GET request to the PokéAPI https://pokeapi.co/api/v2/pokemon. (This API requires no authentication).
2. 使用“拆分”节点转换 results 字段中的数据。
¥Transform the data in the results field with the Split Out node.
3. 使用“代码”节点转换 results 字段中的数据。
¥Transform the data in the results field with the Code node.
显示解决方案
¥??? note "Show me the solution"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |