{"record":{"id":"ba6111dd9306f5df","repo":"kubernetes/kops","slug":"error-converting-to-json-w","errorCode":null,"errorMessage":"error converting to json: %w","messagePattern":"error converting to json: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/jsonutils/transform.go","lineNumber":133,"sourceCode":"\t\ts[i] = v2\n\t}\n\n\treturn s, nil\n}\n\n// SortSlice sorts a slice of any values, ordered by their JSON representations.\n// This is not very efficient, but is convenient for small slice where we don't know their types.\nfunc SortSlice(s []any) ([]any, error) {\n\ttype entry struct {\n\t\to       any\n\t\tsortKey string\n\t}\n\n\tvar entries []entry\n\tfor i := range s {\n\t\tj, err := json.Marshal(s[i])\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"error converting to json: %w\", err)\n\t\t}\n\t\tentries = append(entries, entry{o: s[i], sortKey: string(j)})\n\t}\n\n\tsort.Slice(entries, func(i, j int) bool {\n\t\treturn entries[i].sortKey < entries[j].sortKey\n\t})\n\n\tout := make([]any, 0, len(s))\n\tfor i := range s {\n\t\tout = append(out, entries[i].o)\n\t}\n\n\treturn out, nil\n}\n\n// visitPrimitive is a helper function that visits a primitive value in the JSON tree\nfunc (o *Transformer) visitPrimitive(v any, _ string) (any, error) {","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/pkg/jsonutils/transform.go#L115-L151","documentation":"SortSlice sorts a []any by each element's JSON representation. If json.Marshal fails for any element — e.g. a channel, func, complex number, or a cyclic reference — the sort aborts with this wrapped error. It is a wrapper around encoding/json's 'json: unsupported type' error.","triggerScenarios":"Calling jsonutils.SortSlice on a slice containing unmarshalable values (chan, func, complex128), a struct with only unmarshalable fields, or a data structure containing a reference cycle.","commonSituations":"Sorting mixed/unknown data where an element accidentally holds a callback or channel; sorting trees built by hand rather than decoded from JSON.","solutions":["Remove or replace unmarshalable elements (chan/func/complex) before sorting.","Marshal-test each element with json.Marshal beforehand to find the offending one.","Use a custom sort key (e.g. fmt.Sprintf(\"%v\", v) or a dedicated field) instead of full JSON marshalling."],"exampleFix":"// before\nsorted, err := jsonutils.SortSlice(items) // items contains a func\n// after\nfor i, v := range items {\n\tif _, err := json.Marshal(v); err != nil {\n\t\treturn fmt.Errorf(\"item %d unmarshalable: %w\", i, err)\n\t}\n}\nsorted, err := jsonutils.SortSlice(items)","handlingStrategy":"validation","validationCode":"for i, v := range s {\n\tif _, err := json.Marshal(v); err != nil {\n\t\treturn fmt.Errorf(\"element %d is not JSON-marshalable: %w\", i, err)\n\t}\n}","typeGuard":null,"tryCatchPattern":"sorted, err := jsonutils.SortSlice(s)\nif err != nil {\n\treturn fmt.Errorf(\"cannot sort slice: %w\", err)\n}","preventionTips":["Keep chan/func/complex values out of data slices.","Build slices by decoding JSON, not by mixing in live Go values.","Pre-marshal each element to surface the offending index."],"tags":["json","sorting","serialization"],"backgroundTag":"json-unsupported-type","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"}