> ## Documentation Index
> Fetch the complete documentation index at: https://docs.fetchhive.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Data Transform

> 通过构建行、串联数组或扁平化数组来重塑工作流数据,无需调用外部服务

# Data Transform

当你需要在另一个步骤读取之前重塑现有工作流输出时,请使用 **Data Transform**。

Data Transform 不调用外部服务。它在 **Google Sheets**、**Airtable**、**Custom API** 以及后续 **Iteration** 步骤之前很有用。

## 配置

| 选项              | 是否必填            | 说明                                                              |
| --------------- | --------------- | --------------------------------------------------------------- |
| Name            | 否               | 步骤在工作流画布中的标签。                                                   |
| Mode            | 是               | 要运行的转换。选择 **Build Rows**、**Concat Arrays** 或 **Flatten Array**。 |
| Fields          | 仅 Build Rows    | 输出对象键及其源值。                                                      |
| Sources         | 仅 Concat Arrays | 要追加到一个数组的数组值。                                                   |
| Source Value    | 仅 Flatten Array | 要扁平化一层的一个数组值。                                                   |
| Mismatch Policy | 仅 Build Rows    | 如何处理长度不同的数组。                                                    |

从 **Search steps...** 的 **Utilities** 分组添加此步骤。

## 模式

### Build Rows

使用 **Build Rows** 按索引将数组打包成一组对象。

每个字段都有 **Key** 和 **Source Value**。键变为对象属性。源可以是完整的变量引用,例如 `{{tool_names}}` 或 `{{step_3.output}}`,或者静态值。

数组源值按索引读取。静态标量值会为每一行重复。

示例:

```json theme={null}
{
  "tool_names": ["Exa", "Airtable"],
  "search_contexts": ["Search context for Exa", "Search context for Airtable"]
}
```

Build Rows 字段:

```json theme={null}
[
  { "key": "tool_name", "value": "{{tool_names}}" },
  { "key": "search_context", "value": "{{search_contexts}}" },
  { "key": "workspace", "value": "Fetch Hive" }
]
```

输出:

```json theme={null}
[
  {
    "tool_name": "Exa",
    "search_context": "Search context for Exa",
    "workspace": "Fetch Hive"
  },
  {
    "tool_name": "Airtable",
    "search_context": "Search context for Airtable",
    "workspace": "Fetch Hive"
  }
]
```

当数组长度不同时,使用 **Mismatch Policy**:

| 策略          | 行为                            |
| ----------- | ----------------------------- |
| Pad null    | 使用最长数组,并用 `null` 填充缺失值。这是默认值。 |
| Strict fail | 如果数组长度不匹配,则步骤失败。              |
| Truncate    | 使用最短的数组长度。                    |

### Concat Arrays

使用 **Concat Arrays** 将多个数组追加为一个更长的数组。

每个源必须解析为数组。如果源解析为字符串、对象、数字或布尔值,步骤将失败。

示例 sources:

```json theme={null}
[
  { "value": "{{step_2.output}}" },
  { "value": "{{step_3.output}}" }
]
```

如果这些值为 `["a", "b"]` 和 `["c"]`,则输出为:

```json theme={null}
["a", "b", "c"]
```

### Flatten Array

使用 **Flatten Array** 将一个数组扁平化一层。

嵌套数组元素会展开。非数组元素保持原位。

示例 source:

```json theme={null}
[[1, 2], 3, [4]]
```

输出:

```json theme={null}
[1, 2, 3, 4]
```

## 输出

Data Transform 将其结果存储在:

```text theme={null}
{{STEP_IDENTIFIER.output}}
```

对于 **Build Rows**,输出是一个对象数组。这种形状非常适合期望记录或行的集成步骤。

对于 **Concat Arrays** 和 **Flatten Array**,输出是后续步骤可以遍历的数组。

## 备注

* 源值应该是完整的占位符,例如 `{{step_2.output}}`,或者静态标量值。
* Data Transform V1 不支持混合模板字符串,例如 `Research for {{tool_names}}`。
* Data Transform 将数组和对象保留为结构化 JSON,供下游步骤使用。
* Data Transform 是免费的实用步骤。

另见:[Google Sheets](../integrations/google-sheets)、[Airtable](../integrations/airtable)、[Custom API](custom-api) 和 [测试与迭代](../../testing-and-iteration)
