flatten()
Recursively traverses nested arrays or collections and flattens all elements into a single-level array.
Syntax
flatten(ARRAY)
Parameters
ARRAY- The array or collection to flatten.
Description
The flatten() function recursively traverses all nested arrays within the input array and returns the result flattened into a top-level array. If an array contains inner arrays, the elements of those inner arrays are extracted and appended to the top-level array. This process repeats until all nesting is resolved.
If the input is a single value rather than an array or collection, the function returns that value as-is. If the input is null, the function returns null.
Error codes
N/A
Usage examples
-
Flatten a simple nested array
json "{}" | eval result = flatten(array(1, array(2, 3), 4)) | # result: [1, 2, 3, 4] -
Flatten an array nested more than two levels deep
json "{}" | eval result = flatten(array(1, array(2, array(3, 4)), 5)) | # result: [1, 2, 3, 4, 5] -
nullinputjson "{}" | eval result = flatten(null) | # result: null
Compatibility
flatten() has been available since before Sonar 4.0.