{"record":{"id":"29c7df9be84b57d7","repo":"googleapis/mcp-toolbox","slug":"error-marshlling-json-v","errorCode":null,"errorMessage":"error marshlling json: %v","messagePattern":"error marshlling json: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/sources/dgraph/dgraph.go","lineNumber":200,"sourceCode":"// postDqlQuery sends a DQL query to the Dgraph server with query, parameters, and optional timeout.\n// Returns the response body ([]byte) and an error, if any.\nfunc (hc *DgraphClient) postDqlQuery(query string, paramsMap map[string]interface{}, timeout string) ([]byte, error) {\n\turlParams := url.Values{}\n\turlParams.Add(\"timeout\", timeout)\n\turl, err := getUrl(hc.baseUrl, \"/query\", urlParams)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tp := struct {\n\t\tQuery     string                 `json:\"query\"`\n\t\tVariables map[string]interface{} `json:\"variables\"`\n\t}{\n\t\tQuery:     query,\n\t\tVariables: paramsMap,\n\t}\n\tbody, err := json.Marshal(p)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error marshlling json: %v\", err)\n\t}\n\n\treq, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(body))\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error building req for endpoint [%v] :%v\", url, err)\n\t}\n\n\treq.Header.Add(\"Content-Type\", \"application/json\")\n\n\treturn hc.doReq(req)\n}\n\n// mutate sends an RDF mutation to the Dgraph server with \"commitNow: true\", embedding parameters.\n// Returns the server's response as a byte slice or an error if the mutation fails.\nfunc (hc *DgraphClient) mutate(mutation string, paramsMap map[string]interface{}) ([]byte, error) {\n\tmu := embedParamsIntoMutation(mutation, paramsMap)\n\tparams := url.Values{}\n\tparams.Add(\"commitNow\", \"true\")","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/sources/dgraph/dgraph.go#L182-L218","documentation":"postDqlQuery marshals the query payload ({Query, Variables}) to JSON before building the HTTP request. This error means json.Marshal failed on that payload. Since it contains a string query and a map of variables, failure typically indicates the params map holds values that cannot be JSON-serialized (e.g. channels, funcs, or cyclic structures).","triggerScenarios":"Calling ExecuteQuery with parameters whose JSON conversion fails — e.g. a paramsMap containing unsupported types after conversion from the incoming parameter map.","commonSituations":"Passing non-JSON-serializable values (time.Time is fine, but custom structs with unexported fields producing empty maps are not; funcs/channels panic-adjacent) through tool parameters; nil maps are fine but exotic values are not.","solutions":["Ensure all query parameters are JSON-serializable primitives (string, number, bool, arrays, objects)","Sanitize/convert parameter values to map[string]any of primitives before calling ExecuteQuery","Log the paramsMap contents to identify the offending value","Check how the tool converts incoming parameters into paramsMap for a type-conversion bug"],"exampleFix":"// before\nparams := map[string]any{\"cb\": func() {}} // not serializable\nbody, err := json.Marshal(p) // fails\n// after\nparams := map[string]any{\"name\": \"alice\", \"limit\": 10} // primitives only\nbody, err := json.Marshal(p)","handlingStrategy":"validation","validationCode":"for k, v := range paramsMap {\n\tif !isJSONSerializable(v) {\n\t\treturn fmt.Errorf(\"parameter %q of type %T is not JSON-serializable\", k, v)\n\t}\n}\n\nfunc isJSONSerializable(v any) bool {\n\t_, err := json.Marshal(v)\n\treturn err == nil\n}","typeGuard":null,"tryCatchPattern":"data, err := svc.ExecuteQuery(ctx, query, params)\nif err != nil && strings.Contains(err.Error(), \"marshlling json\") {\n\t// sanitize params to primitives and retry\n}","preventionTips":["Restrict query parameters to JSON primitives and containers thereof","Pre-serialize parameters in the tool layer to catch bad types early","Test ExecuteQuery with the full set of parameter types your tools accept"],"tags":["dgraph","json","serialization","parameters"],"backgroundTag":"json-marshal-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"}