muesli/duf · error
error formatting the json output: %s
Error message
error formatting the json output: %s
What it means
renderJSON in main.go marshals the collected []Mount slice with json.MarshalIndent before printing. If marshaling fails (the value cannot be represented as JSON), it wraps the error as "error formatting the json output: %s". In this program Mount contains only basic types, so this error is extremely rare and usually indicates an internal invariant violation.
Source
Thrown at main.go:62
width = flag.Uint("width", 0, "max output width")
themeOpt = flag.String("theme", defaultThemeName(), "color themes: dark, light, ansi")
styleOpt = flag.String("style", defaultStyleName(), "style: unicode, ascii")
availThreshold = flag.String("avail-threshold", "10G,1G", "specifies the coloring threshold (yellow, red) of the avail column, must be integer with optional SI prefixes")
usageThreshold = flag.String("usage-threshold", "0.5,0.9", "specifies the coloring threshold (yellow, red) of the usage bars as a floating point number from 0 to 1")
_ = flag.BoolP("human-readable", "h", false, "ignored, just for df compatibility")
inodes = flag.Bool("inodes", false, "list inode information instead of block usage")
jsonOutput = flag.Bool("json", false, "output all devices in JSON format")
warns = flag.Bool("warnings", false, "output all warnings to STDERR")
version = flag.Bool("version", false, "display version")
)
// renderJSON encodes the JSON output and prints it.
func renderJSON(m []Mount) error {
output, err := json.MarshalIndent(m, "", " ")
if err != nil {
return fmt.Errorf("error formatting the json output: %s", err)
}
fmt.Println(string(output))
return nil
}
// parseColumns parses the supplied output flag into a slice of column indices.
func parseColumns(cols string) ([]int, error) {
var i []int
s := strings.Split(cols, ",")
for _, v := range s {
v = strings.TrimSpace(v)
if len(v) == 0 {
continue
}
col, err := stringToColumn(v)View on GitHub (pinned to 4636deb4a7)
Solutions
- Check recently added fields on the Mount struct for types not supported by encoding/json and remove or convert them.
- Inspect the wrapped %s message to identify which value failed to marshal.
- Add a custom MarshalJSON implementation for any field with special encoding needs, ensuring it never returns an error for valid states.
Example fix
// before
type Mount struct {
Device chan string // unsupported
}
// after
type Mount struct {
Device string
} Defensive patterns
Strategy: try-catch
Validate before calling
// ensure Mount fields are JSON-encodable before calling
func validateMounts(m []Mount) error {
_, err := json.Marshal(m)
return err
} Try / catch
if err := renderJSON(mounts); err != nil {
var encErr *json.UnsupportedTypeError
if errors.As(err, &encErr) { /* fix struct field */ }
log.Fatal(err)
} Prevention
- Keep Mount fields restricted to JSON-native types (string, uint64, bool, slices).
- Add a unit test that marshals a sample []Mount.
- Avoid custom MarshalJSON implementations that can fail.
When it happens
Trigger: json.MarshalIndent(m, "", " ") returns an error when marshaling the []Mount slice fails, e.g. if a Mount field ever holds a channel, func, or cyclic data structure unsupported by encoding/json.
Common situations: A developer adds a field of an unencodable type (chan, func, sync.Mutex) to the Mount struct, or a field with a malformed MarshalJSON method that returns an error.
Understand the failure class
Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.
Related errors
- unknown style option: %s
- unknown device group: %s
- error parsing avail-threshold: invalid option '%s'
- error parsing usage-threshold: invalid option '%s'
- reading mountinfo %q: %w
AI-assisted analysis of muesli/duf@4636deb4a7 (2026-09-06).
Data as JSON: /api/errors/906910da3309ea0d.
Report an issue: GitHub.