How to use jsonPath inside array in AWS Step Functions
10,472
Solution 1
Since a new release you could use the intrinsic function States.Array
:
"arrayParameter.$": "States.Array($.output.type)"
https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-intrinsic-functions.html
Solution 2
As @Seth Miller mentioned JsonPath resolution within arrays doesn't work unfortunately. If the amount of values to replace in the array is small and known there's a simple workaround (in my case I needed an array of size 1).
The steps are:
- Initialise the array with the number of values you need;
- Replace each value using
"ResultPath": "$.path.to.array[n]"
; - Use
"$.path.to.array"
in your task.
Simple, working example:
{
"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
}
}
}
And to use the resulting array in your task example:
"Second": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:<aws_id>:function:MyFunction",
"Parameters": {
"regularParameter": "some string",
"arrayParameter.$": "$.arrayParameter"
},
"Next": "Succeed"
},
Related videos on Youtube
Comments
-
alexgbelov almost 2 years
I am writing an AWS step function, and for one of the steps, I wish to call a lambda that accepts an array as one of the inputs. However, if I try to pass in a JsonPath into the array, I get
The value for the field 'arrayField.$' must be a STRING that contains a JSONPath but was an ARRAY
My step function definition:
{ "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" } } }
How can I use jsonPath inside the array?
-
alexgbelov over 4 yearsRight, but this way, the contents of the array are constants; I'm looking for a way to include jsonPaths inside the array.
-
WaterKnight over 4 yearsThe output array could be generated dynamically in the first state if it was of Type Task. All you need to do is create a Lambda function for the first state that generates the array dinamically.
-
Dmitry Kolomiets almost 4 yearsRealized that I provided an answer that is very similar to yours. Once you have an array of objects, you can use jsonPath magic to convert it into array of values, in your case it would be: $.arrayParameter[*].type
-
andresv over 3 yearsIt solved my problem when needing one static and one dynamic array element "Args.$": "States.Array('fixed_text',$.ProcessName)"
-
Abdul Haseeb over 3 yearsHi, I want to pass a list of variables "args": ["$.variable1", "$.variable2", "$.variable3"] but it's giving me an error
-
Petar Butkovic over 3 years@AbdulHaseeb Have you tried "args": "States.Array($.variable1, $.variable2, $.variable3)"?
-
ollik1 over 2 yearsThis also works in CDK
step_fn.JsonPath.string_at('States.Array($.output.type)')