AWS Step Function:Function .length()在选择状态下的变量字段中返回错误

Pop*_*net 5 amazon-web-services node.js aws-step-functions

我在AWS Step Function中有一个Choice状态,该状态将比较Input中数组的长度,并决定进入下一个状态。

但是,length()要获取数组长度的函数返回了一个错误:

{
“ error”:“ 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"
            }
         ]
      },
Run Code Online (Sandbox Code Playgroud)

该状态与其他返回JSON的状态连接。JSON如下所示:

{
  "Metadata": [
    {
      "foo": "name"
    },
    {
      "Status": "COMPLETED"
    },
    {
      "Items": []
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

因此,我尝试获取Itemsin 的长度Metadata[2]并进行比较(如果该值大于0)。

我试图验证$.Metadata[2].Items.length()网站中的JsonPath ,它返回0。

我不确定是否错过了任何事情。我在AWS Step Function的文档中或在jsonpath中使用函数的示例中找不到任何信息。

我将不胜感激。谢谢!

Abh*_*eet 10

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

你可以做这样的事情

"CheckItemsCountState":{  
     "Type": "Choice",
     "InputPath": "$",
     "OutputPath": "$",
     "Default": "NoItemsState",
     "Choices":[
       {
         "And": [
           {
            "Variable": "$.Metadata[2].Items",
            "IsPresent": true
           },
           {
             "Variable": "$.Metadata[2].Items[0]",
             "IsPresent": true
           }
         ],
        "Next": "ProcessState"
       }
     ]
  }
Run Code Online (Sandbox Code Playgroud)

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


Joh*_*oom 8

Step Functions 不允许您使用函数来获取值。从选择规则文档

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

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

{
  "Metadata": [
    {
      "foo": "name"
    },
    {
      "Status": "COMPLETED"
    },
    {
      "Items": [],
      "ItemsCount": 0
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

然后在 Step Function Choice 步骤中:

"CheckItemsCountState":{  
    "Type": "Choice",
    "InputPath": "$",
    "OutputPath": "$",
    "Default": "NoItemsState",
    "Choices":[  
        {  
            "Variable": "$.Metadata[2].ItemsCount",
            "NumericGreaterThan": 0,
            "Next": "ProcessState"
        }
    ]
},
Run Code Online (Sandbox Code Playgroud)

  • 这项技术正在发挥作用。我希望 JSONPath 中的函数可以在 Step Function 中使用。顺便说一下,感谢您花时间回答我的问题。 (2认同)

Poo*_*del 5

AWS Step Functions 添加了 14 个内在函数,其中包括States.ArrayLength 解决此问题的函数: https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-intrinsic-functions.html#asl-intrsc -func 数组

例如,给定以下输入数组:

{
   "inputArray": [1,2,3,4,5,6,7,8,9]
}
Run Code Online (Sandbox Code Playgroud)

您可以使用 States.ArrayLength 返回 inputArray 的长度:

"length.$": "States.ArrayLength($.inputArray)"
Run Code Online (Sandbox Code Playgroud)

在此示例中,States.ArrayLength 将返回以下表示数组长度的 JSON 对象:

{ "length": 9 }
Run Code Online (Sandbox Code Playgroud)