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

如何在Step Function中用ASL或C# CDK将标量数组转字典数组?

AWS Step Function 标量数组转字典数组实现方案

用ASL直接实现

Pass状态的简单Parameters映射没法处理数组转换,改用ASL内置的States.ArrayMap函数就能搞定,以下是完整的状态机片段:

{
  "Comment": "将标量数组转换为字典数组",
  "StartAt": "TransformArray",
  "States": {
    "TransformArray": {
      "Type": "Pass",
      "Parameters": {
        "A.$": "$.A",
        "B.$": "$.B",
        "C.$": "States.ArrayMap($.C, (item, index) => {\"Z\": item})"
      },
      "End": true
    }
  }
}
  • A.$和B.$直接复用原输入的字段值,保持不变
  • C.$通过States.ArrayMap遍历原数组$.C,把每个元素item包装成{"Z": item}的字典,最终组合成目标数组

如果你的Step Functions版本不支持内置函数,就用Map状态的备选方案:

{
  "Comment": "兼容旧版本的Map状态转换",
  "StartAt": "MapTransform",
  "States": {
    "MapTransform": {
      "Type": "Map",
      "ItemsPath": "$.C",
      "Iterator": {
        "StartAt": "TransformItem",
        "States": {
          "TransformItem": {
            "Type": "Pass",
            "Parameters": {
              "Z.$": "$"
            },
            "End": true
          }
        }
      },
      "ResultPath": "$.C",
      "Parameters": {
        "A.$": "$.A",
        "B.$": "$.B",
        "C.$": "$.C"
      },
      "End": true
    }
  }
}

用C# CDK实现

在C# CDK里,同样可以利用JsonPath.ArrayMap来构建Pass状态的参数配置,代码示例如下:

using Amazon.CDK.AWS.StepFunctions;
using Constructs;
using Amazon.CDK;

public class ArrayTransformStack : Stack
{
    public ArrayTransformStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props)
    {
        // 定义数组转换的Pass状态
        var transformArray = new Pass(this, "TransformArray", new PassProps
        {
            Parameters = new Dictionary<string, object>
            {
                ["A"] = JsonPath.StringAt("$.A"),
                ["B"] = JsonPath.StringAt("$.B"),
                ["C"] = JsonPath.ArrayMap(
                    JsonPath.StringAt("$.C"),
                    JsonPath.Object(new Dictionary<string, object>
                    {
                        ["Z"] = JsonPath.StringAt(JsonPath.ArrayMapItemIndex())
                    })
                )
            },
            End = true
        });

        // 创建状态机
        new StateMachine(this, "ArrayTransformStateMachine", new StateMachineProps
        {
            DefinitionBody = DefinitionBody.FromChainable(transformArray),
            StateMachineType = StateMachineType.STANDARD
        });
    }
}

如果需要兼容旧版本Step Functions,用Map状态的C# CDK代码:

using Amazon.CDK.AWS.StepFunctions;
using Constructs;
using Amazon.CDK;

public class ArrayTransformStack : Stack
{
    public ArrayTransformStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props)
    {
        // 单个元素转换的Pass状态
        var transformItem = new Pass(this, "TransformItem", new PassProps
        {
            Parameters = new Dictionary<string, object>
            {
                ["Z"] = JsonPath.StringAt("$")
            },
            End = true
        });

        // Map状态遍历数组并转换
        var mapTransform = new Map(this, "MapTransform", new MapProps
        {
            ItemsPath = JsonPath.StringAt("$.C"),
            Iterator = transformItem,
            ResultPath = "$.C",
            Parameters = new Dictionary<string, object>
            {
                ["A"] = JsonPath.StringAt("$.A"),
                ["B"] = JsonPath.StringAt("$.B"),
                ["C"] = JsonPath.StringAt("$.C")
            },
            End = true
        });

        // 创建状态机
        new StateMachine(this, "ArrayTransformStateMachine", new StateMachineProps
        {
            DefinitionBody = DefinitionBody.FromChainable(mapTransform),
            StateMachineType = StateMachineType.STANDARD
        });
    }
}

内容的提问来源于stack exchange,提问作者Ahmed Hassan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 15:50:38