{"record":{"id":"75cbb9fe742814a8","repo":"googleapis/mcp-toolbox","slug":"failed-to-marshal-param-to-json-w","errorCode":null,"errorMessage":"failed to marshal param to JSON: %w","messagePattern":"failed to marshal param to JSON: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/util/parameters/common.go","lineNumber":76,"sourceCode":"\tcase \"boolean\":\n\t\ttempSlice := make([]bool, len(s))\n\t\tfor j, item := range s {\n\t\t\tb, ok := item.(bool)\n\t\t\tif !ok {\n\t\t\t\treturn nil, fmt.Errorf(\"expected item at index %d to be boolean, got %T\", j, item)\n\t\t\t}\n\t\t\ttempSlice[j] = b\n\t\t}\n\t\ttypedSlice = tempSlice\n\t}\n\treturn typedSlice, nil\n}\n\n// convertParamToJSON  is a Go template helper function to convert a parameter to JSON formatted string.\nfunc convertParamToJSON(param any) (string, error) {\n\tjsonData, err := json.Marshal(param)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to marshal param to JSON: %w\", err)\n\t}\n\treturn string(jsonData), nil\n}\n\n// PopulateTemplateWithJSON populate a Go template with a custom `json` array formatter\nfunc PopulateTemplateWithJSON(templateName, templateString string, data map[string]any) (string, error) {\n\treturn PopulateTemplateWithFunc(templateName, templateString, data, template.FuncMap{\n\t\t\"json\": convertParamToJSON,\n\t})\n}\n\n// PopulateTemplate populate a Go template with no custom formatters\nfunc PopulateTemplate(templateName, templateString string, data map[string]any) (string, error) {\n\treturn PopulateTemplateWithFunc(templateName, templateString, data, nil)\n}\n\n// PopulateTemplateWithFunc populate a Go template with provided functions\nfunc PopulateTemplateWithFunc(templateName, templateString string, data map[string]any, funcMap template.FuncMap) (string, error) {","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/util/parameters/common.go#L58-L94","documentation":"convertParamToJSON is a Go template helper that marshals a parameter value to a JSON-formatted string via json.Marshal. json.Marshal only fails for values it cannot encode — channels, funcs, maps with non-string keys, cyclic structures — so this error indicates a template parameter holds an unmarshalable value.","triggerScenarios":"Rendering a SQL/tool template with PopulateTemplate/PopulateTemplateWithJSON where the data map contains e.g. a channel, a function, or a cyclic nested value passed as a parameter.","commonSituations":"Passing a Go func or channel into template data by mistake; using map[customKey]any with non-basic key types; self-referencing structures built programmatically.","solutions":["Inspect the parameter map and remove/replace the unmarshalable value (channel, func, cyclic ref)","Convert custom map key types to string keys before templating","Ensure parameters originate from decoded request data (JSON-safe types)","Log the marshalled type (%T) to pinpoint which param fails"],"exampleFix":"// before\ndata[\"cb\"] = func() {} // unmarshalable\nresult, err := PopulateTemplateWithJSON(\"t\", tmpl, data)\n// after\ndata[\"cb\"] = nil\nresult, err := PopulateTemplateWithJSON(\"t\", tmpl, data)","handlingStrategy":"validation","validationCode":"func jsonSafe(v any) error {\n    _, err := json.Marshal(v)\n    return err\n}\n// call jsonSafe(param) for each template data value before templating","typeGuard":"func isJSONMarshalable(v any) bool {\n    return json.Valid(mustMarshal(v)) || marshalableKind(v)\n}\nfunc marshalableKind(v any) bool {\n    switch v.(type) {\n    case chan struct{}, func():\n        return false\n    }\n    return true\n}","tryCatchPattern":"out, err := PopulateTemplateWithJSON(name, tmpl, data)\nif err != nil {\n    if strings.Contains(err.Error(), \"marshal param to JSON\") {\n        return nil, fmt.Errorf(\"template parameter not JSON-serializable: %w\", err)\n    }\n    return nil, err\n}","preventionTips":["Only pass JSON-decoded (JSON-safe) values as template parameters","Avoid channels, funcs, and cyclic references in template data","Use string-keyed maps for parameters"],"tags":["json","templates","serialization"],"backgroundTag":"json-serialization-failed","analyzedSha":"8cc6e09de2ad7b8bffc77751799585a1401a48eb","analyzedAt":"2026-09-05T01:10:36.887Z","contentChangedAt":"2026-09-05T01:10:36.887Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}