You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何转换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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 00:20:29