Skip to content

数据转换的方法(Approaches for transforming data)#

n8n中的数据转换涉及在数据通过工作流时对其进行修改、重组或增强。这包括更改数据格式、筛选或汇总数值、添加计算字段,以及转换数据结构以适应不同的节点。

🌐 Data transformation in n8n involves modifying, restructuring, or enriching data as it moves through your workflow. This includes changing data formats, filtering or aggregating values, adding computed fields, and converting data structures to work with different nodes.

n8n 使用一个预定义的 数据结构,允许所有节点正确处理传入的数据。当你的数据不符合此结构,或者当你需要根据你的用例修改它时,你需要对其进行转换。

🌐 n8n uses a predefined data structure that allows all nodes to process incoming data correctly. When your data doesn't match this structure, or when you need to modify it for your use case, you'll need to transform it.

n8n 提供了几种数据转换的方法:

🌐 n8n provides several approaches for data transformation:

  • 表达式 允许你使用 n8n 的表达式语法({{ }})直接在节点参数中转换数据
  • 代码节点 让你可以编写自定义 JavaScript 或 Python 以进行复杂的转换。
  • AI转换节点 从自然语言提示生成转换代码。
  • 高级转换技术:对于复杂的数据操作,n8n 支持:
  • 三元运算符:在表达式中直接使用条件逻辑 (condition ? valueIfTrue : valueIfFalse)
  • 链式函数:组合多个转换函数
  • 复杂表达式:在表达式语法中使用 JavaScript 方法和运算符
  • 用于常见结构转换的专用转换节点:
  • 聚合:将分散的物品汇集在一起
  • 限制:限制物品的数量
  • 删除重复项:消除相同的项目
  • 排序:对项目排序或随机化
  • 拆分:将列表分成单独的项目
  • 汇总:像 Excel 数据透视表一样汇总数据

有关这些方法的比较,请参见 表达式与数据节点

🌐 For a comparison of these approaches, see Expressions versus data nodes.