{"record":{"id":"906910da3309ea0d","repo":"muesli/duf","slug":"error-formatting-the-json-output-s","errorCode":null,"errorMessage":"error formatting the json output: %s","messagePattern":"error formatting the json output: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"main.go","lineNumber":62,"sourceCode":"\twidth    = flag.Uint(\"width\", 0, \"max output width\")\n\tthemeOpt = flag.String(\"theme\", defaultThemeName(), \"color themes: dark, light, ansi\")\n\tstyleOpt = flag.String(\"style\", defaultStyleName(), \"style: unicode, ascii\")\n\n\tavailThreshold = flag.String(\"avail-threshold\", \"10G,1G\", \"specifies the coloring threshold (yellow, red) of the avail column, must be integer with optional SI prefixes\")\n\tusageThreshold = 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\")\n\n\t_          = flag.BoolP(\"human-readable\", \"h\", false, \"ignored, just for df compatibility\")\n\tinodes     = flag.Bool(\"inodes\", false, \"list inode information instead of block usage\")\n\tjsonOutput = flag.Bool(\"json\", false, \"output all devices in JSON format\")\n\twarns      = flag.Bool(\"warnings\", false, \"output all warnings to STDERR\")\n\tversion    = flag.Bool(\"version\", false, \"display version\")\n)\n\n// renderJSON encodes the JSON output and prints it.\nfunc renderJSON(m []Mount) error {\n\toutput, err := json.MarshalIndent(m, \"\", \" \")\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error formatting the json output: %s\", err)\n\t}\n\n\tfmt.Println(string(output))\n\treturn nil\n}\n\n// parseColumns parses the supplied output flag into a slice of column indices.\nfunc parseColumns(cols string) ([]int, error) {\n\tvar i []int\n\n\ts := strings.Split(cols, \",\")\n\tfor _, v := range s {\n\t\tv = strings.TrimSpace(v)\n\t\tif len(v) == 0 {\n\t\t\tcontinue\n\t\t}\n\n\t\tcol, err := stringToColumn(v)","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/muesli/duf/blob/4636deb4a7b707a9f04c602db033f9837e50b3f6/main.go#L44-L80","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\ntype Mount struct {\n\tDevice chan string // unsupported\n}\n// after\ntype Mount struct {\n\tDevice string\n}","handlingStrategy":"try-catch","validationCode":"// ensure Mount fields are JSON-encodable before calling\nfunc validateMounts(m []Mount) error {\n\t_, err := json.Marshal(m)\n\treturn err\n}","typeGuard":null,"tryCatchPattern":"if err := renderJSON(mounts); err != nil {\n\tvar encErr *json.UnsupportedTypeError\n\tif errors.As(err, &encErr) { /* fix struct field */ }\n\tlog.Fatal(err)\n}","preventionTips":["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."],"tags":["go","json","serialization"],"backgroundTag":"json-marshal-failed","analyzedSha":"4636deb4a7b707a9f04c602db033f9837e50b3f6","analyzedAt":"2026-09-06T02:31:58.576Z","contentChangedAt":"2026-09-06T02:31:58.576Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}