

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 在 Step Functions 中使用 ResultPath 指定狀態輸出
<a name="input-output-resultpath"></a>

**管理狀態和轉換資料**  
此頁面是指 JSONPath。Step Functions 最近新增了變數和 JSONata 來管理狀態和轉換資料。  
了解如何[使用變數傳遞資料](workflow-variables.md)，以及[使用 JSONata 轉換資料](transforming-data.md)。

狀態的輸出可以是其輸入的複本、所產生的結果 (例如，從 `Task` 狀態之 Lambda 函數的輸出)，或輸入和結果的結合。使用 `ResultPath` 以控制哪些組合會傳遞至狀態輸出。

以下狀態類型可以產生結果，並可包含 `ResultPath:`
+ [傳遞工作流程狀態](state-pass.md)
+ [任務工作流程狀態](state-task.md)
+ [平行工作流程狀態](state-parallel.md)
+ [映射工作流程狀態](state-map.md)

使用 `ResultPath` 合併任務結果與任務輸入，或選取其中一個。您提供給 `ResultPath` 的路徑會控制哪些資訊會傳遞到輸出。

**注意**  
 `ResultPath` 僅限於使用[參考路徑](amazon-states-language-paths.md#amazon-states-language-reference-paths)，這會限制範圍，因此路徑必須僅識別 JSON 中的單一節點。請參閱 [Amazon States Language](concepts-amazon-states-language.md) [參考路徑](amazon-states-language-paths.md#amazon-states-language-reference-paths)中的 。

## 使用 ResultPath 將輸入取代為任務結果
<a name="input-output-resultpath-default"></a>

如果您未指定 `ResultPath`，預設行為會與 相同`"ResultPath": "$"`。狀態會將整個狀態輸入取代為任務的結果。

```
# State Input
{  
 "comment": "This is a test",
 "details": "Default example",
 "who" : "Step Functions"
}

# Path 
"ResultPath": "$"

# Task result
"Hello, Step Functions!"

# State Output
"Hello, Step Functions!"
```

**注意**  
`ResultPath` 是在傳遞至輸出之前，用於在輸入中包含來自結果的內容。但是，如果`ResultPath`未指定 ，則預設動作是取代整個輸入。

## 捨棄結果並保留原始輸入
<a name="input-output-resultpath-null"></a>

如果您將 `ResultPath`設定為 `null`，狀態會將**原始輸入**傳遞至輸出。狀態的輸入承載會直接複製到輸出，而不考慮任務結果。

```
# State Input
{  
 "comment": "This is a test",
 "details": "Default example",
 "who" : "Step Functions"
}

# Path 
"ResultPath": null

# Task result
"Hello, Step Functions!"

# State Output
{  
 "comment": "This is a test",
 "details": "Default example",
 "who" : "Step Functions"
}
```

## 使用 ResultPath 在輸入中包含結果
<a name="input-output-resultpath-append"></a>

如果您為 ResultPath 指定路徑，狀態輸出將結合狀態輸入和任務結果：

```
# State Input
{  
 "comment": "This is a test",
 "details": "Default example",
 "who" : "Step Functions"
}

# Path 
"ResultPath": "$.taskresult"

# Task result
"Hello, Step Functions!"

# State Output
{  
 "comment": "This is a test",
 "details": "Default example",
 "who" : "Step Functions",
 "taskresult" : "Hello, Step Functions!"
}
```

您也可以在子節點的輸入中插入結果。將 `ResultPath` 設定為下列。

```
"ResultPath": "$.strings.lambdaresult"
```

根據下列輸入：

```
{
  "comment": "An input comment.",
  "strings": {
    "string1": "foo",
    "string2": "bar",
    "string3": "baz"
  },
  "who": "AWS Step Functions"
}
```

任務結果會插入為輸入中`strings`節點的子項。

```
{
  "comment": "An input comment.",
  "strings": {
    "string1": "foo",
    "string2": "bar",
    "string3": "baz",
    "lambdaresult": "Hello, Step Functions!"
  },
  "who": "AWS Step Functions"
}
```

狀態輸入現在會將原始輸入 JSON 做為子節點包含在結果中。

## 使用 ResultPath 以結果更新輸入中的節點
<a name="input-output-resultpath-amend"></a>

如果您為 ResultPath 指定現有節點，任務結果將取代該現有節點：

```
# State Input
{  
 "comment": "This is a test",
 "details": "Default example",
 "who" : "Step Functions"
}

# Path 
"ResultPath": "$.comment"

# Task result
"Hello, Step Functions!"

# State Output
{  
 "comment": "Hello, Step Functions!",
 "details": "Default example",
 "who" : "Step Functions"
}
```

## 使用 ResultPath 在 中同時包含錯誤和輸入 `Catch`
<a name="input-output-resultpath-catch"></a>

在某些情況下，您可能想要保留原始輸入的錯誤。在 `Catch` 中使用 `ResultPath` 以包含錯誤與原始輸入，而不是將其取代。

```
"Catch": [{ 
  "ErrorEquals": ["States.ALL"], 
  "Next": "NextTask", 
  "ResultPath": "$.error" 
}]
```

如果之前的 `Catch` 陳述式截獲錯誤，它會在狀態輸入內的 `error` 節點包含結果。例如，透過以下輸入：

```
{"foo": "bar"}
```

在截獲錯誤時的狀態輸出如下。

```
{
  "foo": "bar",
  "error": {
    "Error": "{{Error here}}"
  }
}
```

如需錯誤處理的詳細資訊，請參閱以下內容：
+ [處理 Step Functions 工作流程中的錯誤](concepts-error-handling.md)
+ [在 Step Functions 狀態機器中處理錯誤條件](tutorial-handling-error-conditions.md)