{"record":{"id":"df2de8ccba97f5a9","repo":"kubernetes/kops","slug":"error-writing-json-to-stdout-v","errorCode":null,"errorMessage":"error writing json to stdout: %v","messagePattern":"error writing json to stdout: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/kops/get_all.go","lineNumber":136,"sourceCode":"\t\tfor _, group := range instancegroups {\n\t\t\tallObjects = append(allObjects, group)\n\t\t}\n\t\tfor _, additionalObject := range addonObjects {\n\t\t\tallObjects = append(allObjects, additionalObject)\n\t\t}\n\t}\n\n\tswitch options.Output {\n\tcase OutputYaml:\n\t\tif err := fullOutputYAML(out, allObjects...); err != nil {\n\t\t\treturn fmt.Errorf(\"error writing yaml to stdout: %v\", err)\n\t\t}\n\n\t\treturn nil\n\n\tcase OutputJSON:\n\t\tif err := fullOutputJSON(out, false, allObjects...); err != nil {\n\t\t\treturn fmt.Errorf(\"error writing json to stdout: %v\", err)\n\t\t}\n\t\treturn nil\n\n\tcase OutputTable:\n\t\tfmt.Fprintf(out, \"Cluster\\n\")\n\t\terr = clusterOutputTable([]*api.Cluster{cluster}, out)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tfmt.Fprintf(out, \"\\nInstance Groups\\n\")\n\t\terr = igOutputTable(cluster, instancegroups, out)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif len(addonObjects) != 0 {\n\t\t\tfmt.Fprintf(out, \"\\nAddon Objects\\n\")\n\t\t\terr = addonsOutputTable(cluster, addonObjects, out)\n\t\t\tif err != nil {","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/cmd/kops/get_all.go#L118-L154","documentation":"Wraps any error from fullOutputJSON, which serializes the fetched objects to JSON and writes them to stdout. This mirrors error 342 for the JSON path; the underlying failure is either a marshaling/codec error or a write error on the output stream.","triggerScenarios":"`kops get all <cluster> -o json` where object serialization via the codecs fails (unregistered kind, version conversion) or the write to stdout fails.","commonSituations":"State-store objects from a newer kops version with fields the current codecs reject; piping to a closed pipe; scripting `kops get all -o json` and consuming output while the writer errors.","solutions":["Inspect the wrapped inner error for the marshal vs write distinction","Align kops CLI version with the cluster state-store version","Use `--name`/full cluster name and rerun; verify state store readability","Write to a file instead of a pipe to avoid write failures"],"exampleFix":"// before\nj, err := fullOutputJSON(out, false, allObjects...)\nif err != nil { return fmt.Errorf(\"error writing json to stdout: %v\", err) }\n// after\nif err := fullOutputJSON(buf, false, allObjects...); err != nil { return err }\n_, err = out.Write(buf.Bytes())","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := fullOutputJSON(out, false, allObjects...); err != nil {\n    if errors.Is(err, syscall.EPIPE) {\n        return nil // downstream consumer closed stdout\n    }\n    return fmt.Errorf(\"error writing json to stdout: %v\", err)\n}","preventionTips":["Consume full output instead of truncating with head","Parse with jq only after the command exits 0","Align kops CLI version with state-store version","Redirect to file when output is large"],"tags":["json","output","serialization","cli"],"backgroundTag":"json-marshal-failed","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T07:17:12.445Z"}