Amazon web services 如何在AWS Step函数中使用数组内部的jsonPath
我正在编写一个AWS step函数,对于其中一个步骤,我希望调用一个lambda,它接受数组作为输入之一。但是,如果我尝试将JsonPath传递到数组中,我会得到Amazon web services 如何在AWS Step函数中使用数组内部的jsonPath,amazon-web-services,aws-step-functions,Amazon Web Services,Aws Step Functions,我正在编写一个AWS step函数,对于其中一个步骤,我希望调用一个lambda,它接受数组作为输入之一。但是,如果我尝试将JsonPath传递到数组中,我会得到 The value for the field 'arrayField.$' must be a STRING that contains a JSONPath but was an ARRAY 我的step函数定义: { "StartAt": "First", "States": { "First": { "T
The value for the field 'arrayField.$' must be a STRING that contains a JSONPath but was an ARRAY
我的step函数定义:
{
"StartAt": "First",
"States": {
"First": {
"Type": "Pass",
"Parameters": {
"type": "person"
},
"ResultPath": "$.output",
"Next": "Second"
},
"Second": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:<aws_id>:function:MyFunction",
"Parameters": {
"regularParameter": "some string",
"arrayParameter.$": ["$.output.type"]
},
"Next": "Succeed"
},
"Succeed": {
"Type": "Succeed"
}
}
}
{
“StartAt”:“First”,
“国家”:{
“第一”:{
“类型”:“通过”,
“参数”:{
“类型”:“人”
},
“结果路径”:“$.output”,
“下一个”:“第二个”
},
“第二”:{
“类型”:“任务”,
“资源”:“arn:aws:lambda:us-east-1::function:MyFunction”,
“参数”:{
“regularParameter”:“某些字符串”,
“arrayParameter.$”:[“$.output.type”]
},
“下一步”:“成功”
},
“成功”:{
“类型”:“成功”
}
}
}
如何在数组中使用jsonPath?jsonPath INDER parameters字段必须是字符串。因此,如果您想将一个名为arrayParameter的参数传递给lambda函数,则需要进行jsonPath查询以提取该数组 例如,如果在键输出中有一个名为outputArray的键,其值为数组 输入JSON:
{
"pre": "sdigf",
"output": {
"result": 1,
"outputArray": ["test1","test2","test.."]
}
}
参数sintax:
"arrayParameter.$": "$.output.outputArray"
合理的建议
我今天在阵列中运行了一个JsonPath解析用例,发现(像您一样)该功能现在不存在。我最终决定在代码中进行数据处理更简单、更干净。例如,您可以创建一个小Lambda,该Lambda接收First
发出的对象,并将其转换为Second
可以接受的格式,然后将其添加到输出中(WaterKnight在一篇文章中提到了此解决方案)
这假设由于某种原因,您无法在秒内将输入的格式更改为该Lambda(这将是此处的绝对最短路径)
无理劝告
也就是说,如果您想要一种完全在相当粗糙的Step函数中实现这一点的方法,那么可以使用执行的。映射状态的输出是一个数组,用于聚合每个组成过程状态的输出。这些过程状态仅使用参数
属性在最终数组中发出所需的值。下面是一个示例步骤函数定义。我确实警告过,这很糟糕,我用了不同的方法来解决这个问题
{
"StartAt": "First",
"Comment": "Please don't actually do this",
"States": {
"First": {
"Type": "Pass",
"Parameters": {
"type": "person"
},
"ResultPath": "$.output",
"Next": "Add Array"
},
"Add Array": {
"Comment": "A Map state needs some array to loop over in order to work. We will give it a dummy array. Add an entry for each value you intend to have in the final array. The values here don't matter.",
"Type": "Pass",
"Result": [
0
],
"ResultPath": "$.dummy",
"Next": "Mapper"
},
"Mapper": {
"Comment": "Add a Pass state with the appropriate Parameters for each field you want to map into the output array",
"Type": "Map",
"InputPath": "$",
"ItemsPath": "$.dummy",
"Parameters": {
"output.$": "$.output"
},
"Iterator": {
"StartAt": "Massage",
"States": {
"Massage": {
"Type": "Pass",
"Parameters": {
"type.$": "$.output.type"
},
"OutputPath": "$.type",
"End": true
}
}
},
"ResultPath": "$.output.typeArray",
"Next": "Second"
},
"Second": {
"Comment": "The Lambda in your example is replaced with Pass so that I could test this",
"Type": "Pass",
"Parameters": {
"regularParameter": "some string",
"arrayParameter.$": "$.output.typeArray"
},
"Next": "Succeed"
},
"Succeed": {
"Type": "Succeed"
}
}
}
正如@Seth Miller提到的,阵列内的JsonPath解析很不幸无法工作。如果数组中要替换的值量很小并且已知,那么有一个简单的解决方法(在我的例子中,我需要一个大小为1的数组)
这些步骤是:
使用所需的值数初始化数组李>
使用“ResultPath”:“$.path.to.array[n]”替换每个值李>
在任务中使用“$.path.to.array”
”
简单的工作示例:
{
"StartAt": "First",
"States": {
"First": {
"Type": "Pass",
"Parameters": {
"type": "person"
},
"ResultPath": "$.output",
"Next": "Initialise Array"
},
"Initialise Array": {
"Comment": "Add an entry for each value you intend to have in the final array, the values here don't matter.",
"Type": "Pass",
"Parameters": [
0
],
"ResultPath": "$.arrayParameter",
"Next": "Fill Array"
},
"Fill Array": {
"Comment": "Replace the first entry of array with parameter",
"Type": "Pass",
"InputPath": "$.output.type",
"ResultPath": "$.arrayParameter[0]",
"End": true
}
}
}
要在任务示例中使用生成的数组,请执行以下操作:
"Second": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:<aws_id>:function:MyFunction",
"Parameters": {
"regularParameter": "some string",
"arrayParameter.$": "$.arrayParameter"
},
"Next": "Succeed"
},
“第二个”:{
“类型”:“任务”,
“资源”:“arn:aws:lambda:us-east-1::function:MyFunction”,
“参数”:{
“regularParameter”:“某些字符串”,
“arrayParameter.$”:“$.arrayParameter”
},
“下一步”:“成功”
},
正如许多答案正确指出的那样,不可能完全按照您需要的方式来做。但我建议另一种解决方案:一系列字典。这并不完全是你所需要的,但它是本地的,不黑
"Second": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:<aws_id>:function:MyFunction",
"Parameters": {
"regularParameter": "some string",
"arrayParameter": [{"type.$": "$.output.type"}]
},
"Next": "Succeed"
},
另一种方法是使用并行状态输出对象数组,然后使用jsonPath将其转换为简单数组:
{
"StartAt": "Parallel",
"States": {
"Parallel": {
"Type": "Parallel",
"Next": "Use Array",
"ResultPath": "$.items",
"Branches": [
{
"StartAt": "CreateArray",
"States": {
"CreateArray": {
"Type": "Pass",
"Parameters": {
"value": "your value"
},
"End": true
}
}
}
]
},
"Use Array": {
"Type": "Pass",
"Parameters": {
"items.$": "$.items[*].value"
},
"End": true
}
}
}
在本例中,并行状态输出以下json:
{
"items": [
{
"value": "your value"
}
]
}
“使用数组”状态产生:
{
"items": [
"your value"
]
}
自新版本以来,您可以使用内在函数States.Array
:
"arrayParameter.$": "States.Array($.output.type)"
对,但这样,数组的内容是常量;我正在寻找一种在数组中包含JSONPATH的方法。如果输出数组是Task类型的,那么它可以在第一个状态下动态生成。你所需要做的就是为第一个动态生成数组的状态创建一个Lambda函数。一旦有了一个对象数组,就可以使用jsonPath magic将其转换为值数组,在您的例子中是:$.arrayParameter[*].typeIt解决了我在需要一个静态和一个动态数组元素“Args.$”:“States.array('fixed_text',$.ProcessName')时遇到的问题。您好,我想传递一个变量列表“Args”:[“$.variable1”,“$.variable2”,“$.variable3”]但它给了我一个error@AbdulHaseeb您是否尝试过“args”:“States.Array($.variable1,$.variable2,$.variable3)”?
"arrayParameter.$": "States.Array($.output.type)"