ItemsPath (地圖,僅限 JSONPath) - AWS Step Functions

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

ItemsPath (地圖,僅限 JSONPath)

管理狀態和轉換資料

此頁面是指 JSONPath。Step Functions 最近新增了變數和 JSONata 來管理狀態和轉換資料。

了解如何使用變數傳遞資料,以及使用 JSONata 轉換資料

在以 JSONPath 為基礎的狀態下,使用 ItemsPath 欄位在提供給 Map 狀態的 JSON 輸入內選取陣列或物件。根據預設,Map狀態會ItemsPath設定為 $,這會選取整個輸入。

  • 如果Map狀態的輸入是 JSON 陣列,它會針對陣列中的每個項目執行反覆運算,將該項目傳遞給反覆運算做為輸入

  • 如果Map狀態的輸入是 JSON 物件,它會針對物件中的每個鍵值對執行反覆運算,並將該對傳遞給反覆運算做為輸入

注意

只有在您使用從工作流程中先前狀態傳遞ItemsPathJSON 輸入時,才能在分散式映射狀態下使用 。

的值ItemsPath必須是參考路徑,且該路徑必須評估為 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。在InputPath篩選輸入之後,它會在狀態的有效輸入上操作。

如需 Map 狀態的詳細資訊,請參閱下列內容: