mikefarah/yq · error
node at path [%v] is not an array (it's a %v)
Error message
node at path [%v] is not an array (it's a %v)
What it means
The `reverse` operator only works on arrays (sequence nodes). When a matched candidate is not a SequenceNode, yq returns this error naming the node's path and tag, so you know exactly which node failed. Note it returns the original context alongside the error, so no partial results are produced for that expression run.
Source
Thrown at pkg/yqlib/operator_reverse.go:15
package yqlib
import (
"container/list"
"fmt"
)
func reverseOperator(_ *dataTreeNavigator, context Context, _ *ExpressionNode) (Context, error) {
results := list.New()
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
if candidate.Kind != SequenceNode {
return context, fmt.Errorf("node at path [%v] is not an array (it's a %v)", candidate.GetNicePath(), candidate.Tag)
}
reverseList := candidate.CreateReplacementWithComments(SequenceNode, "!!seq", candidate.Style)
reverseContent := make([]*CandidateNode, len(candidate.Content))
for i, originalNode := range candidate.Content {
reverseContent[len(candidate.Content)-i-1] = originalNode
}
reverseList.AddChildren(reverseContent)
results.PushBack(reverseList)
}
return context.ChildContext(results), nil
}
View on GitHub (pinned to 8b5af0694b)
Solutions
- Select the array first: `.items | reverse` instead of `reverse` on the whole map.
- If reversing map keys, use `keys | reverse` and then re-map, e.g. `to_entries | reverse | from_entries`.
- Guard with `select(tag == "!!seq") | reverse` to only reverse arrays.
- Use the path in the error to locate and fix the offending node or expression.
Example fix
// before yq 'reverse' file.yml # root is a map // after yq '.items | reverse' file.yml
Defensive patterns
Strategy: type-guard
Validate before calling
yq -e 'tag == "!!seq"' <<< "$node" || echo "reverse requires an array"
Type guard
isSeq() { [ "$(yq 'tag == "!!seq"' <<< "$1")" = "true" ]; } Try / catch
out=$(yq 'select(tag == "!!seq") | reverse' file.yml 2>&1) || { echo "$out"; echo "use to_entries | reverse | from_entries for maps"; } Prevention
- Select the array before reversing: .items | reverse.
- Reverse map key order with keys | reverse or to_entries | reverse | from_entries.
- Guard multi-doc pipelines with select(tag == "!!seq").
- Use the path in the error message to locate the non-array node.
When it happens
Trigger: `yq 'reverse'` on a document whose root is a map or scalar; `reverse` applied to a map-valued key like `.myMap | reverse`; multi-document input where one document is not an array.
Common situations: Reversing a map by mistake (expecting key order reversal — use `keys | reverse` or `to_entries` instead); schema changes turning an array into an object; selecting the parent container instead of the array field.
Related errors
- cannot pick indices from type %v (%v)
- cannot pivot node of type %v
- %v (%v) cannot be subtracted from %v
- %v cannot check contained in %v
- from entries only runs against arrays
AI-assisted analysis of mikefarah/yq@8b5af0694b (2026-09-05).
Data as JSON: /api/errors/50f65d4fdd2221d4.
Report an issue: GitHub.