mikefarah/yq · error

from entries only runs against arrays

Error message

from entries only runs against arrays

What it means

The `from_entries` operator converts an array of {key, value} objects into a map. It only accepts SequenceNode inputs; when a candidate document node is any other kind (map, scalar, null), fromEntriesOperator returns this error. It exists to stop users from applying an array-only transform to non-array data.

Source

Thrown at pkg/yqlib/operator_entries.go:118

	node.Kind = MappingNode
	node.Tag = "!!map"
	return node, nil
}

func fromEntriesOperator(_ *dataTreeNavigator, context Context, _ *ExpressionNode) (Context, error) {
	var results = list.New()
	for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
		candidate := el.Value.(*CandidateNode)

		switch candidate.Kind {
		case SequenceNode:
			mapResult, err := fromEntries(candidate)
			if err != nil {
				return Context{}, err
			}
			results.PushBack(mapResult)
		default:
			return Context{}, fmt.Errorf("from entries only runs against arrays")
		}
	}

	return context.ChildContext(results), nil
}

func withEntriesOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {

	//to_entries on the context
	toEntries, err := toEntriesOperator(d, context, expressionNode)
	if err != nil {
		return Context{}, err
	}

	var results = list.New()

	for el := toEntries.MatchingNodes.Front(); el != nil; el = el.Next() {
		candidate := el.Value.(*CandidateNode)

View on GitHub (pinned to 8b5af0694b)

Solutions

  1. Pipe the input through to_entries first if you have a map: `. | to_entries | ... | from_entries`
  2. Wrap scalars into an array context or correct the expression so input is an array
  3. Check for null input (empty file/missing key) and provide a default: `. // [] | from_entries`
  4. Use map_values/map instead if the intent was transforming a map, not rebuilding one

Example fix

// before
{"a": 1} | from_entries
// error: from entries only runs against arrays

// after
{"a": 1} | to_entries | from_entries
Defensive patterns

Strategy: type-guard

Validate before calling

yq 'select(kind == "seq") // empty' input.yaml

Type guard

def is_array_node(node):
    return node is not None and getattr(node, 'kind', None) == 'seq'

Try / catch

if ! out=$(yq 'from_entries' file.yaml 2>&1); then
  echo "from_entries needs an array input: $out" >&2
fi

Prevention

When it happens

Trigger: Calling `from_entries` directly on an object (e.g. `{a: 1} | from_entries`), on a scalar, or when the piped input is null/empty rather than an array of entry objects.

Common situations: Users confuse from_entries with to_entries and apply it to a map; a previous expression unexpectedly yields a scalar; reading a top-level JSON object and trying to convert it to entries.

Related errors


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