

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

# ItemsPath （地圖，僅限 JSONPath)
<a name="input-output-itemspath"></a>

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

在以 JSONPath 為基礎的狀態下，使用 `ItemsPath` 欄位在提供給 `Map` 狀態的 JSON 輸入內選取陣列或物件。根據預設，`Map`狀態會`ItemsPath`設定為 `$`，這會選取整個輸入。
+  如果`Map`狀態的輸入是 JSON 陣列，它會針對陣列中的每個項目執行反覆運算，將該項目傳遞給反覆運算做為輸入 
+  如果`Map`狀態的輸入是 JSON 物件，它會針對物件中的每個鍵值對執行反覆運算，並將該對傳遞給反覆運算做為輸入 

**注意**  
只有在您使用從工作流程中先前狀態傳遞`ItemsPath`的 *JSON 輸入時，才能在分散式映射*狀態下使用 。

的值`ItemsPath`必須是[參考路徑](amazon-states-language-paths.md#amazon-states-language-reference-paths)，且該路徑必須評估為 JSON 陣列或物件。例如，請考慮包含兩個陣列的 `Map` 狀態輸入，如下列範例所示。

```
{
  "ThingsPiratesSay": [
    {
      "say": "Avast!"
    },
    {
      "say": "Yar!"
    },
    {
      "say": "Walk the Plank!"
    }
  ],
  "ThingsGiantsSay": [
    {
      "say": "Fee!"
    },
    {
      "say": "Fi!"
    },
    {
      "say": "Fo!"
    },
    {
      "say": "Fum!"
    }
  ]
}
```

在此情況下，您可以使用 選取要用於`Map`狀態反覆運算的陣列`ItemsPath`。下列狀態機器定義會使用 指定輸入中的`ThingsPiratesSay`陣列`ItemsPath`。然後，它會針對`ThingsPiratesSay`陣列中的每個項目執行`SayWord`傳遞狀態的反覆運算。

```
{
  "StartAt": "PiratesSay",
  "States": {
    "PiratesSay": {
      "Type": "Map",
      "ItemsPath": "$.ThingsPiratesSay",
      "ItemProcessor": {
         "StartAt": "SayWord",
         "States": {
           "SayWord": {
             "Type": "Pass",
             "End": true
           }
         }
      },
      "End": true
    }
  }
}
```

對於巢狀 JSON 物件，您可以使用 在輸入內`ItemsPath`選取特定物件。考慮以下具有巢狀組態資料的輸入：

```
{
  "environment": "production",
  "servers": {
    "web": {
      "server1": {"port": 80, "status": "active"},
      "server2": {"port": 8080, "status": "inactive"}
    },
    "database": {
      "primary": {"host": "db1.example.com", "port": 5432},
      "replica": {"host": "db2.example.com", "port": 5432}
    }
  }
}
```

若要逐一查看 Web 伺服器物件，請將 `ItemsPath`設定為 `$.servers.web`：

```
{
  "StartAt": "ProcessWebServers",
  "States": {
    "ProcessWebServers": {
      "Type": "Map",
      "ItemsPath": "$.servers.web",
      "ItemProcessor": {
         "StartAt": "CheckServer",
         "States": {
           "CheckServer": {
             "Type": "Pass",
             "End": true
           }
         }
      },
      "End": true
    }
  }
}
```

處理輸入時，`Map`狀態會在 `ItemsPath`之後套用[`InputPath`](input-output-inputpath-params.md#input-output-inputpath)。在`InputPath`篩選輸入之後，它會在狀態的有效輸入上操作。

如需 `Map` 狀態的詳細資訊，請參閱下列內容：
+  [對應狀態](state-map.md) 
+ [映射狀態處理模式](state-map.md#concepts-map-process-modes)
+ [使用內嵌映射重複動作](tutorial-map-inline.md)
+ [內嵌`Map`狀態輸入和輸出處理](state-map-inline.md#inline-map-state-output)