mikefarah/yq · error

node at path [%v] is not an array or map (it's a %v)

Error message

node at path [%v] is not an array or map (it's a %v)

What it means

The sort/sort_by operators require each matched node to be an array or map so there are values to order — after visiting values (or when the node cannot be visited) the kind check fails and the node's path and actual tag are reported. Sorting a scalar or null is undefined, hence the guard.

Source

Thrown at pkg/yqlib/operator_sort.go:44

		var sortableArray sortableNodeArray

		if candidate.CanVisitValues() {
			sortableArray = make(sortableNodeArray, 0)
			visitor := func(valueNode *CandidateNode) error {
				compareContext, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(valueNode), expressionNode.RHS)
				if err != nil {
					return err
				}
				sortableNode := sortableNode{Node: valueNode, CompareContext: compareContext, dateTimeLayout: context.GetDateTimeLayout()}
				sortableArray = append(sortableArray, sortableNode)
				return nil
			}
			if err := candidate.VisitValues(visitor); err != nil {
				return context, err
			}
		} else {
			return context, fmt.Errorf("node at path [%v] is not an array or map (it's a %v)", candidate.GetNicePath(), candidate.Tag)
		}

		sort.Stable(sortableArray)

		sortedList := candidate.CopyWithoutContent()
		switch candidate.Kind {
		case MappingNode:
			for _, sortedNode := range sortableArray {
				sortedList.AddKeyValueChild(sortedNode.Node.Key, sortedNode.Node)
			}
		case SequenceNode:
			for _, sortedNode := range sortableArray {
				sortedList.AddChild(sortedNode.Node)
			}
		}

		// convert array of value nodes back to map
		results.PushBack(sortedList)

View on GitHub (pinned to 8b5af0694b)

Solutions

  1. Apply sort only to arrays/maps: .items | sort_by(.x)
  2. Wrap scalars: [.] | sort
  3. Filter to collection nodes first: select(type == "!!seq" or type == "!!map")
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at pkg/yqlib/operator_sort.go:44 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of mikefarah/yq@8b5af0694b (2026-09-05). Data as JSON: /api/errors/ecd943d8483899c0. Report an issue: GitHub.