【问题标题】:Is it possible to have a single parameter receive its value conditionally in a step function?是否可以让单个参数在阶跃函数中有条件地接收其值?
【发布时间】:2025-11-26 01:45:01
【问题描述】:

我有一个参数“request_id”,我想从两种不同格式的 JSON 中获取一个 ID,并发送到我的步进函数任务的输入。

第一个表单如下所示:

{ "request_id": "abcde-abcd-abcde-abc" }

第二种是这种形式:

{ "request": { "id": "abcde-abcd-abcde-abc", }

目前,我有一个看起来像的参数

"request_id.$": "$.request_id"

但想要相当于(这个不行)的东西

"request_id.$": "$.['request_id','request.id']"

这是否可以在 step 函数中实现,或者我需要将这两个请求 ID 拆分为 JSON 中的两个路径还是在函数中执行?

【问题讨论】:

    标签: json amazon-web-services yaml jsonpath aws-step-functions


    【解决方案1】:

    一种解决方法可能是有一个选择状态并检查第一个变量是否存在:

    {
        "Variable": "$.request_id",
        "IsPresent": true
    }
    

    然后根据结果进行两次不同的赋值。

    https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-choice-state.html

    {
      "StartAt": "choice",
      "States": {
        "choice": {
          "Type": "Choice",
          "Choices": [
            {
              "Variable": "$.request_id",
              "IsPresent": true,
              "Next": "assignment1"
            }
          ],
          "Default": "assignment2"
        },
        "assignment1": {
          "Type": "Pass",
          "Result": "World",
          "End": true
        },
        "assignment2": {
          "Type": "Pass",
          "Result": "World",
          "End": true
        }
      }
    }
    

    【讨论】:

    • 我正在研究这个问题,但我不确定如何根据存在的任务来分配任务,除非我有两个相同的任务。
    • @AlecShern 我更新了我的答案并添加了一个使用选择状态的示例。