

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Step Functions で ResultPath を使用してステートの出力を指定する
<a name="input-output-resultpath"></a>

**ステートの管理とデータの変換**  
このページは JSONPath に関するものです。Step Functions では最近、ステート管理とデータ変換のために変数と JSONata が追加されました。  
[変数を使用したデータ受け渡し](workflow-variables.md)と [JSONata を使用したデータ変換](transforming-data.md)について説明します。

状態の出力は、入力のコピー、生成される結果 (例: `Task` 状態の Lambda 関数からの出力)、またはその入力と結果の組み合わせです。`ResultPath` を使用して、上記のうち、状態出力に渡す組み合わせを制御します。

次の状態タイプでは、結果を生成できます。また、`ResultPath:` を含むことができます。
+ [Pass ワークフロー状態](state-pass.md)
+ [Task ワークフロー状態](state-task.md)
+ [Parallel ワークフローの状態](state-parallel.md)
+ [Map 状態のワークフロー](state-map.md)

`ResultPath` を使用して、タスク結果とタスク入力を組み合わせるか、またはこれらのいずれかを選択します。`ResultPath` に指定したパスで、出力に渡す情報を制御します。

**注記**  
 `ResultPath` では、[参照パス](amazon-states-language-paths.md#amazon-states-language-reference-paths)の使用に限定されており、そのパスにより JSON 内の単一ノードのみを識別するように範囲が制限されます。[Amazon ステートメント言語](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)