googleapis/mcp-toolbox · error
error parsing JSON: %v
Error message
error parsing JSON: %v
What it means
RunSQL posts a DQL query to the Dgraph /query endpoint and unmarshals the response body into a struct with a Data field. This error means the HTTP response body was not valid JSON (or not the expected shape). Dgraph typically returns JSON, so this usually indicates an error page, empty body, or a non-JSON response (proxy/auth layer).
Source
Thrown at internal/sources/dgraph/dgraph.go:138
}
func (s *Source) RunSQL(statement string, params parameters.ParamValues, isQuery bool, timeout string) (any, error) {
paramsMap := params.AsMapWithDollarPrefix()
resp, err := s.DgraphClient().ExecuteQuery(statement, paramsMap, isQuery, timeout)
if err != nil {
return nil, err
}
if err := checkError(resp); err != nil {
return nil, err
}
var result struct {
Data map[string]interface{} `json:"data"`
}
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("error parsing JSON: %v", err)
}
return result.Data, nil
}
func initDgraphHttpClient(ctx context.Context, tracer trace.Tracer, r Config) (*DgraphClient, error) {
//nolint:all // Reassigned ctx
ctx, span := sources.InitConnectionSpan(ctx, tracer, SourceType, r.Name)
defer span.End()
if r.DgraphUrl == "" {
return nil, fmt.Errorf("dgraph url should not be empty")
}
hc := &DgraphClient{
httpClient: &http.Client{},
baseUrl: r.DgraphUrl,
HttpToken: &HttpToken{View on GitHub (pinned to 8cc6e09de2)
Solutions
- Log the raw response body and HTTP status code before unmarshaling to see what was actually returned
- Verify DgraphUrl points to the correct Dgraph HTTP port (default 8080), not gRPC (9080)
- Check for proxies/ingress returning non-JSON error pages
- Return the raw body in the error message to aid debugging
Example fix
// before
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("error parsing JSON: %v", err)
}
// after
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("error parsing JSON: %v (body: %s)", err, string(resp))
} Defensive patterns
Strategy: validation
Validate before calling
u, err := url.Parse(dgraphUrl)
if err != nil || u.Scheme == "" || u.Host == "" {
return fmt.Errorf("dgraphUrl must be a valid http(s) URL")
}
// health-check before querying:
// resp, err := http.Get(dgraphUrl + "/health") Try / catch
data, err := svc.RunSQL(ctx, query)
if err != nil && strings.Contains(err.Error(), "error parsing JSON") {
// inspect raw response: wrong endpoint, proxy error page, or auth failure
} Prevention
- Point dgraphUrl at the Dgraph HTTP port (8080), not gRPC (9080)
- Include the raw response body in error logs for diagnosis
- Health-check the Dgraph endpoint during startup
When it happens
Trigger: Calling RunSQL when the Dgraph server (or an intermediary) returns a non-JSON body: empty response, HTML error page from a reverse proxy, plain-text auth failure, or truncated response.
Common situations: Wrong DgraphUrl pointing at a non-Dgraph service, an ingress/load balancer returning 502 HTML, TLS termination issues returning an error string, Dgraph returning gzipped body handled incorrectly.
Understand the failure class
Background: "Invalid JSON response" and "Failed to parse response" errors: when an API answers 200 but the body isn't the JSON your library expected — this error's family across 28 libraries.
Related errors
- failed to unmarshal json: %w, body: %s
- error marshlling json: %v
- failed to marshal credentials: %v
- error building req for endpoint [%v] : %v
- failed to unmarshal response: %v
AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05).
Data as JSON: /api/errors/86c143664637a8d4.
Report an issue: GitHub.