tomnomnom/gron · error

failed to convert statements to JSON

Error message

failed to convert statements to JSON

What it means

After merging all parsed statements, the ungron function's json.Encoder.Encode failed to serialize the merged result for display. Since the merged value comes from successfully parsed JSON tokens, this is practically a hard failure (exit code exitJSONEncode) and indicates an unencodable value slipped into the merged tree.

Source

Thrown at main.go:385

	// If there's only one top level key and it's "json", make that the top level thing
	mergedMap, ok := merged.(map[string]interface{})
	if ok {
		if len(mergedMap) == 1 {
			if _, exists := mergedMap["json"]; exists {
				merged = mergedMap["json"]
			}
		}
	}

	// Marshal the output into JSON to display to the user
	out := &bytes.Buffer{}
	enc := json.NewEncoder(out)
	enc.SetIndent("", "  ")
	enc.SetEscapeHTML(false)
	err = enc.Encode(merged)
	if err != nil {
		return exitJSONEncode, errors.Wrap(err, "failed to convert statements to JSON")
	}
	j := out.Bytes()

	// If the output isn't monochrome, add color to the JSON
	if opts&optMonochrome == 0 {
		c, err := colorizeJSON(j)

		// If we failed to colorize the JSON for whatever reason,
		// we'll just fall back to monochrome output, otherwise
		// replace the monochrome JSON with glorious technicolor
		if err == nil {
			j = c
		}
	}

	// For whatever reason, the monochrome version of the JSON
	// has a trailing newline character, but the colorized version
	// does not. Strip the whitespace so that neither has the newline

View on GitHub (pinned to 88a6234ea2)

Solutions

  1. Inspect the merged structure for values JSON cannot encode (e.g. channels, funcs, or unsupported types added upstream)
  2. Check the underlying wrapped error to identify which value failed to encode
Defensive patterns

Strategy: fallback

When it happens

Trigger: Thrown at main.go:385 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of tomnomnom/gron@88a6234ea2 (2026-09-06). Data as JSON: /api/errors/40e2e30dbc89be4c. Report an issue: GitHub.