9

我在 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"
            }
         ]
      },

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

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

所以我试图获取 in 的长度ItemsMetadata[2]在值大于 0 时进行比较。

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

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

我将不胜感激。谢谢!

4

2 回答 2

9

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

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

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

{
  "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"
        }
    ]
},
于 2017-10-02T02:55:10.907 回答
1

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

你可以做这样的事情

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

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

于 2021-04-22T13:25:29.703 回答