charmbracelet/crush · error

parse error: %w

Error message

parse error: %w

What it means

readInputs in the jq builtin wraps a JSON decoding failure. When reading jq inputs without -R (raw input), the stream is decoded as one or more JSON values via json.Decoder; any decode error other than io.EOF (trailing garbage, invalid JSON) is returned wrapped as "parse error: <detail>".

Source

Thrown at internal/shell/jq.go:282

					}
				}
			}
			continue
		}

		// Decode potentially multiple JSON values from the stream.
		dec := json.NewDecoder(strings.NewReader(string(data)))
		var streamVals []any
		for {
			if err := ctx.Err(); err != nil {
				return nil, err
			}
			var v any
			if err := dec.Decode(&v); err != nil {
				if err == io.EOF {
					break
				}
				return nil, fmt.Errorf("parse error: %w", err)
			}
			streamVals = append(streamVals, v)
		}

		if slurp {
			vals = append(vals, streamVals)
		} else {
			vals = append(vals, streamVals...)
		}
	}

	if len(vals) == 0 {
		return []any{nil}, nil
	}
	return vals, nil
}

// ctxReader wraps an io.Reader so that each Read call checks ctx first.

View on GitHub (pinned to 7944b8e522)

Solutions

  1. Validate the input is well-formed JSON (e.g. `jq . input.json`) and fix the producer or the data.
  2. If inputs are raw text, not JSON, add the raw-input flag to the jq invocation.
  3. Strip non-JSON noise (log lines, BOM) before piping into the jq tool.
  4. Check the wrapped json error's offset message to locate the exact malformed byte.

Example fix

// before
cat app.log | crush jq '.msg'
// after
cat app.log | grep '^{' | crush jq '.msg'
Defensive patterns

Strategy: validation

Validate before calling

var probe any
if err := json.Unmarshal(trimmedInput, &probe); err != nil {
    // not valid JSON — fix the producer or switch to raw-input mode
}

Try / catch

if err != nil {
    if strings.HasPrefix(err.Error(), "parse error: ") {
        // JSON decode failure in jq input; use wrapped offset to locate it
    }
}

Prevention

When it happens

Trigger: handleJQ receives input (file or stdin) in JSON mode; dec.Decode hits malformed JSON such as `{foo}`, truncated documents, concatenated values with stray separators, or invalid UTF-8, while the context is still live.

Common situations: Piping output of a command that emits logs mixed with JSON; feeding empty files with stray whitespace or BOM; passing HTML error pages instead of JSON; forgetting -R when inputs are raw text lines.

Understand the failure class

Related errors


AI-assisted analysis of charmbracelet/crush@7944b8e522 (2026-08-29). Data as JSON: /api/errors/19b9d34a69556e91. Report an issue: GitHub.