【问题标题】:AWS Step Function: Function .length() returned error in variable field in Choice stateAWS Step Function:函数 .length() 在选择状态的变量字段中返回错误
【发布时间】:2021-04-22 21:25:29
【问题描述】:

我在 AWS Step Function 中有一个选择状态,它将比较输入中的数组长度并决定进入下一个状态。

但是,length() 函数在获取数组的长度时返回错误:

{
“错误”:“States.Runtime”,
"cause": "执行状态 'CheckItemsCountState' 时发生错误(在事件 id #18 处输入)。无效路径 '$.Metadata[2].Items.length()':选择状态的条件路径引用无效价值。”

}

选择状态的定义如下:

     "CheckItemsCountState":{  
         "Type": "Choice",
         "InputPath": "$",
         "OutputPath": "$",
         "Default": "NoItemsState",
         "Choices":[  
            {  
               "Variable": "$.Metadata[2].Items.length()",
               "NumericGreaterThan": 0,
               "Next": "ProcessState"
            }
         ]
      },

该状态已连接到返回 JSON 的其他状态。 JSON如下:

{
  "Metadata": [
    {
      "foo": "name"
    },
    {
      "Status": "COMPLETED"
    },
    {
      "Items": []
    }
  ]
}

所以我试图在Metadata[2] 中获取Items 的长度,并在值大于0时进行比较。

我已尝试验证此 website 中的 JsonPath $.Metadata[2].Items.length() 并返回 0。

我不确定我是否遗漏了什么。我在 AWS Step Function 的文档或在 jsonpath 中使用函数的示例中找不到任何信息。

如果有任何帮助,我将不胜感激。谢谢!

【问题讨论】:

  • 只是为了正确理解问题,包含“MetaData”和“Items”的 JSON 是从 aws step 函数控制台复制的确切 json?

标签: node.js amazon-web-services aws-step-functions


【解决方案1】:

Step Functions 不允许您使用函数来获取值。来自Choice Rules documentation

对于这些运算符中的每一个,对应的值必须是 适当的类型:字符串、数字、布尔值或时间戳。

要按照您的要求进行操作,您需要在前一个函数中获取数组长度并将其作为输出的一部分返回。

{
  "Metadata": [
    {
      "foo": "name"
    },
    {
      "Status": "COMPLETED"
    },
    {
      "Items": [],
      "ItemsCount": 0
    }
  ]
}

然后在 Step Function Choice 步骤中:

"CheckItemsCountState":{  
    "Type": "Choice",
    "InputPath": "$",
    "OutputPath": "$",
    "Default": "NoItemsState",
    "Choices":[  
        {  
            "Variable": "$.Metadata[2].ItemsCount",
            "NumericGreaterThan": 0,
            "Next": "ProcessState"
        }
    ]
},

【讨论】:

  • 这项技术很有效。我希望 JSONPath 中的函数可以在 Step Function 中使用。顺便说一句,感谢您抽出宝贵时间回答我的问题。
【解决方案2】:

如果您只想检查它是否为非空数组,一种可能的方法是检查零索引。

你可以这样做

"CheckItemsCountState":{  
     "Type": "Choice",
     "InputPath": "$",
     "OutputPath": "$",
     "Default": "NoItemsState",
     "Choices":[
       {
         "And": [
           {
            "Variable": "$.Metadata[2].Items",
            "IsPresent": true
           },
           {
             "Variable": "$.Metadata[2].Items[0]",
             "IsPresent": true
           }
         ],
        "Next": "ProcessState"
       }
     ]
  }

另一种方法是在接受的答案中提到,在上一个函数中设置计数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多