Flatten Array of Arrays using JSONata for AWS Step Functions

// 1 comment

The Map state of AWS Step Functions returns an array of arrays as a result. For further processing, we often need to flatten this result into a simple one-dimensional flat array. With JSONPath, this can be accomplished using the [*] expression as described in the AWS docs:

"ResultSelector": { "flattenArray.$": "$[*][*]" }

Recently, AWS Step Functions introduced JSONata as an alternative query language to JSONPath. However, since this feature is quite new, you won't find much information on how to accomplish the same with JSONata. At least, this section didn't make it into the AWS docs for JSONata yet.

Note that I'm using states instead of $states in the following examples, because the $ indicates a variable but $states doesn't exist in the context of the JSONata Exerciser.

In JSONata you can use the .* expression to flatten an array of arrays:

JSONata
JSONata Exerciser

However, you must be aware that this expression returns nothing if the result array is empty:

JSONata
JSONata Exerciser

In JSONata, you can usually append [] to an expression to cast the result to an array, even if it has only one value:

JSONata
JSONata Exerciser

However, this trick doesn't work in this case as both [].* and .*[] will return nothing:

JSONata
JSONata Exerciser

The correct way to handle it is a simple ternary condition using the $exists() function:

JSONata
JSONata Exerciser

If the expression $exists(states.result.*) evaluates to nothing (falsy value), it will return an empty array as a fallback.

You may ask yourself if there is a shorter version using a short circuit || or null coalescing ?? operator like in JavaScript: states.result.* ?? [].

Unfortunately, this remains an open issue.