8

I have a task state that outputs the following:

"batch": {
    "batch": "size",
    "currentTimestamp": 1596205376
  },

and a map state out outputs an array:

"batch": [
    {
      "batch": "product-batch-0",
      "currentTimestamp": 1596205376
    },
    {
      "batch": "product-batch-1",
      "currentTimestamp": 1596205376
    }
]

I would like to combine them so that the input to the state that follows the map state is this:

  "batch": [
    {
    "batch": "Size",
    "currentTimestamp": 1596205376
    },
    {
      "batch": "product-batch-22",
      "currentTimestamp": 1596205376
    },
    {
      "batch": "product-batch-8",
      "currentTimestamp": 1596205376
    }
]

Is this possible using the input/output processing available in aws step functions? I want to have them contained in one array so they can be processed together in an additional map state later in the state machine.

3 Answers 3

17

It is possible. try this.

  • ASL Definition
{
  "StartAt": "getArrayOfArray",
  "States": {
    "getArrayOfArray": {
      "Type": "Pass",
      "Parameters": {
        "arrayOfArray.$": "States.Array($.array, States.Array($.appendant))"
      },
      "Next": "mergeArray"
    },
    "mergeArray": {
      "Type": "Pass",
      "Parameters": {
        "mergedArray.$": "$.arrayOfArray[*][*]"
      },
      "End": true
    }
  }
}
  • input
{
  "array": [
    {
      "batch": "product-batch-0",
      "currentTimestamp": 1596205376
    },
    {
      "batch": "product-batch-1",
      "currentTimestamp": 1596205376
    }
  ],
  "appendant": {
    "batch": "size",
    "currentTimestamp": 1596205376
  }
}
  • output
{
  "mergedArray": [
    {
      "batch": "product-batch-0",
      "currentTimestamp": 1596205376
    },
    {
      "batch": "product-batch-1",
      "currentTimestamp": 1596205376
    },
    {
      "batch": "size",
      "currentTimestamp": 1596205376
    }
  ]
}
Sign up to request clarification or add additional context in comments.

3 Comments

Can you explain why this syntax results in the final mergedArray? "mergedArray.$": "$.arrayOfArray[*][*]" Are you using the [*][*] syntax to flatten the nested arrays created in the "Pass" state?
Exactly. Amazon States Language (ASL) follows JSONPath syntax. ref: aws.amazon.com/blogs/compute/… JSONPath syntax has the wildcard expression. The wildcard enables to retrieve all objects/elements regardless their names. ref: goessner.net/articles/JsonPath/index.html
Thanks a lot for this solution, it solved my current need and will probably solve future needs as well
2

You should use another Lambda function to merge the input and output because the step functions output feature cannot append the result into an array or merge the input and output as an array.

[1] : https://docs.aws.amazon.com/step-functions/latest/dg/input-output-resultpath.html#input-output-resultpath-append

Comments

0

In case you need to do it several times in the step function you can use only one state using array flattening (some_array[*][*]) along with array merging (States.Array(array1, array2)) at the same time:

  "AddItemToArrayState": {
  "Type": "Pass",
  "Next": "NextState,
  "Parameters": {
    "property_with_array.$": "States.Array($.property_with_array[*][*], States.Array('item1', 'item2'))"
  }
},
"NextState": {
  "Type": "Pass",
  "Next": "FinalState,
  "Parameters": {
    "property_with_array.$": "States.Array($.property_with_array[*][*], States.Array('item1', 'item2'))"
  }
},
"FinalState": {
  "Type": "Pass",
  "Parameters": {
    "property_with_array.$": "$.property_with_array[*][*]"
  },
  "End": true
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.