ModelWorks logoModelWorks
JSON TransformArray

flatten

Flatten nested arrays up to a configurable depth

flatten

Flattens nested arrays. With depth: 1 (the default) only one level is unwrapped; deeper nesting is preserved.

Options

OptionTypeRequiredDefaultDescription
depthintegerNo1How many levels to flatten. 0 returns input wrapped as-is.

Example

Input

JSON
[[1, 2], [3, [4, 5]], 6]

Operation (depth 1)

JSON
{ "type": "flatten", "options": { "depth": 1 } }

Output

JSON
[1, 2, 3, [4, 5], 6]

Operation (depth 2)

JSON
{ "type": "flatten", "options": { "depth": 2 } }

Output

JSON
[1, 2, 3, 4, 5, 6]

In a Circuit Step

JSON
{
  "name": "flatten_pages",
  "description": "Flatten a paginated list of items into a single array",
  "function": "circuit.core.transform.json",
  "input": {
    "source": "${{ pages }}",
    "operations": [
      { "type": "flatten", "options": { "depth": 1 } }
    ]
  },
  "outputs": [
    {
      "name": "all_items",
      "description": "All items from all pages, in order",
      "value": "${{ flatten_pages.output.transformed_result }}"
    }
  ]
}

Notes & gotchas

  • Unlike most array operations, flatten does not error on non-array input. A scalar input becomes [input].
  • depth: 0 returns [input] unchanged (no flattening occurs).
  • Each level descends through Value::Array only; objects are treated as opaque leaves and not flattened.
  • The output is always an array, even when starting from a scalar.
On this page

On this page