{"record":{"id":"3d14dbe0ea180980","repo":"mikefarah/yq","slug":"only-arrays-are-supported-for-flatten","errorCode":null,"errorMessage":"only arrays are supported for flatten","messagePattern":"only arrays are supported for flatten","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/yqlib/operator_flatten.go","lineNumber":43,"sourceCode":"\t\t\t\tnewSeq = append(newSeq, content[i].Content[j])\n\t\t\t}\n\t\t} else {\n\t\t\tnewSeq = append(newSeq, content[i])\n\t\t}\n\t}\n\tnode.Content = make([]*CandidateNode, 0)\n\tnode.AddChildren(newSeq)\n}\n\nfunc flattenOp(_ *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {\n\n\tlog.Debugf(\"flatten Operator\")\n\tdepth := expressionNode.Operation.Preferences.(flattenPreferences).depth\n\n\tfor el := context.MatchingNodes.Front(); el != nil; el = el.Next() {\n\t\tcandidate := el.Value.(*CandidateNode)\n\t\tif candidate.Kind != SequenceNode {\n\t\t\treturn Context{}, fmt.Errorf(\"only arrays are supported for flatten\")\n\t\t}\n\n\t\tflatten(candidate, depth)\n\n\t}\n\n\treturn context, nil\n\n}\n","sourceCodeStart":25,"sourceCodeEnd":53,"githubUrl":"https://github.com/mikefarah/yq/blob/8b5af0694bb82b41d4ae180fac9972029066f90a/pkg/yqlib/operator_flatten.go#L25-L53","documentation":"The `flatten` operator collapses nested arrays into a single array. It requires each candidate node to be a SequenceNode; if the node is a map, scalar, or null, the operator aborts with this error. flatten does not descend into or flatten object values, only sequences.","triggerScenarios":"Running `flatten` on a map (`{a: [1,2]}`), on a scalar, on null, or on a mixed collection where some matched nodes aren't arrays.","commonSituations":"Applying flatten to the whole document instead of a specific array field; expecting object-of-arrays to be flattened; input from JSON where the top level is an object; empty files producing null nodes.","solutions":["Select the array first: `.myList | flatten` instead of `flatten` on the root object","Convert object values to a sequence first: `[.[] | arrays] | flatten` or `[.[]] | flatten`","Guard nulls: `.data // [] | flatten`","If you need to flatten values inside objects, explode them with to_entries/toarray first"],"exampleFix":"// before\n{a: [1, [2]]} | flatten\n// error: only arrays are supported for flatten\n\n// after\n{a: [1, [2]]} | .a | flatten   # => [1, 2]","handlingStrategy":"type-guard","validationCode":"yq 'select(kind == \"seq\") // empty' input.yaml","typeGuard":"def as_seq(v):\n    return v if isinstance(v, list) else []","tryCatchPattern":"out=$(yq '.data // [] | flatten' f.yaml 2>&1) || {\n  echo \"flatten target is not an array: $out\" >&2\n}","preventionTips":["Select the array field before flattening","Guard null paths with // []","Remember flatten works on sequences only, not object values","Check kind == \"seq\" in expressions that may see mixed data"],"tags":["yq","flatten","type-mismatch"],"backgroundTag":"operator-requires-array-input","analyzedSha":"8b5af0694bb82b41d4ae180fac9972029066f90a","analyzedAt":"2026-09-05T10:57:22.766Z","contentChangedAt":"2026-09-05T10:57:22.766Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}