如何转换JSON输入以适配AWS Step Functions的Map步骤调用
处理输入JSON以适配AWS Step Functions的Map步骤
针对你需要将包含多邮箱的输入转换为可传入Map步骤的数组需求,这里提供两种基于AWS Step Functions内置能力的解决方案,无需额外依赖Lambda等服务:
方法1:提前构造目标数组(用Transform状态)
在Map步骤前添加一个Transform状态,通过内置函数遍历邮箱列表并生成所需的对象数组:
{ "Type": "Transform", "Parameters": { "processedItems.$": "States.Array(States.ForEach($.mail.destination, (email) => States.Merge($$, { \"email_address\": email })))" }, "ResultPath": "$", "Next": "YourMapStep" }
- 逻辑说明:
States.ForEach遍历$.mail.destination中的每个邮箱地址- 对每个邮箱,用
States.Merge($$, { "email_address": email })将原始完整输入与新增的email_address字段合并 States.Array将遍历结果转为数组,最终输出会把生成的数组放在$.processedItems字段下
- 后续Map步骤只需设置
ItemsPath: "$.processedItems"即可迭代处理每个对象
方法2:直接在Map步骤中构造输入(更简洁)
无需提前生成数组,直接在Map步骤中配置ItemSelector,将原始输入与当前迭代的邮箱地址合并:
{ "Type": "Map", "ItemsPath": "$.mail.destination", "ItemSelector": { "email_address.$": "$$.Map.Item.Value", "notificationType.$": "$.notificationType", "mail.$": "$.mail" }, "ItemProcessor": { "ProcessorConfig": { "Mode": "DISTRIBUTED", "ExecutionType": "STANDARD" }, "StartAt": "InvokeSubStepFunction", "States": { "InvokeSubStepFunction": { "Type": "Task", "Resource": "arn:aws:states:::states:startExecution.sync", "Parameters": { "StateMachineArn": "arn:aws:states:你的区域:你的账号ID:stateMachine:子状态机名称", "Input.$": "$" }, "End": true } } }, "End": true }
- 逻辑说明:
ItemsPath指定Map迭代的数据源是$.mail.destination数组ItemSelector为每个迭代项构造输入:$$.Map.Item.Value获取当前迭代的邮箱地址,赋值给email_address- 直接引用原始输入的
notificationType和mail字段,保留完整原始数据
- 每个子Step Function收到的输入就是你期望的格式,一步到位
内容的提问来源于stack exchange,提问作者Valery Zavalna
相关产品推荐
相关产品推荐

