{"record":{"id":"ba1a9f4dfce9a6d0","repo":"grafana/k6","slug":"unmarshalling-response-body-to-json-w","errorCode":null,"errorMessage":"unmarshalling response body to JSON: %w","messagePattern":"unmarshalling response body to JSON: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/http.go","lineNumber":769,"sourceCode":"\tr.extraHeadersMu.RUnlock()\n\treturn headers\n}\n\n// JSON returns the response body as JSON data.\nfunc (r *Response) JSON() (any, error) {\n\tif r.cachedJSON != nil {\n\t\treturn r.cachedJSON, nil\n\t}\n\tif err := r.fetchBody(); err != nil {\n\t\treturn nil, fmt.Errorf(\"getting response body: %w\", err)\n\t}\n\n\tr.bodyMu.RLock()\n\tdefer r.bodyMu.RUnlock()\n\n\tvar v any\n\tif err := json.Unmarshal(r.body, &v); err != nil {\n\t\treturn nil, fmt.Errorf(\"unmarshalling response body to JSON: %w\", err)\n\t}\n\tr.cachedJSON = v\n\n\treturn v, nil\n}\n\n// Ok returns true if status code of response if considered ok, otherwise returns false.\nfunc (r *Response) Ok() bool {\n\tif r.status == 0 || (r.status >= 200 && r.status <= 299) {\n\t\treturn true\n\t}\n\treturn false\n}\n\n// Request returns the request that led to this response.\nfunc (r *Response) Request() *Request {\n\treturn r.request\n}","sourceCodeStart":751,"sourceCodeEnd":787,"githubUrl":"https://github.com/grafana/k6/blob/8d06114777191d21ef885d05c5f8aa9e1ed838ae/internal/js/modules/k6/browser/common/http.go#L751-L787","documentation":"Wrap of json.Unmarshal in Response.JSON: the response body bytes were fetched successfully, but they are not valid JSON (trailing data, HTML error page, BOM, or empty body). The wrapped error pinpoints the JSON syntax problem at its exact offset; results are cached, so only the first JSON() call pays the parse cost and only malformed bodies ever trigger this error.","triggerScenarios":"Thrown at internal/js/modules/k6/browser/common/http.go:770 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Inspect response.text() to see what the server actually returned","Guard with response.ok/status checks before parsing as JSON","Handle non-JSON content types (HTML/XML) with text() instead of json()"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"8d06114777191d21ef885d05c5f8aa9e1ed838ae","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}