{"record":{"id":"ccf0705c0be53d22","repo":"grafana/k6","slug":"the-body-is-null-so-we-can-t-transform-it-to-json","errorCode":null,"errorMessage":"the body is null so we can't transform it to JSON - this likely was because of a request error getting the response","messagePattern":"the body is null so we can't transform it to JSON - this likely was because of a request error getting the response","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"js/modules/k6/http/response.go","lineNumber":70,"sourceCode":"\t}\n\n\tsel, err := html.ParseHTML(rt, body)\n\tif err != nil {\n\t\tcommon.Throw(rt, err)\n\t}\n\tsel.URL = res.URL\n\tif len(selector) > 0 {\n\t\tsel = sel.Find(selector[0])\n\t}\n\treturn sel\n}\n\n// JSON parses the body of a response as JSON and returns it to the Sobek VM.\nfunc (res *Response) JSON(selector ...string) sobek.Value {\n\trt := res.client.moduleInstance.vu.Runtime()\n\n\tif res.Body == nil {\n\t\terr := fmt.Errorf(\"the body is null so we can't transform it to JSON\" +\n\t\t\t\" - this likely was because of a request error getting the response\")\n\t\tcommon.Throw(rt, err)\n\t}\n\n\thasSelector := len(selector) > 0\n\tif res.cachedJSON == nil || hasSelector { //nolint:nestif\n\t\tvar v any\n\n\t\tbody, err := common.ToBytes(res.Body)\n\t\tif err != nil {\n\t\t\tcommon.Throw(rt, err)\n\t\t}\n\n\t\tif hasSelector {\n\t\t\tif !res.validatedJSON {\n\t\t\t\tif !gjson.ValidBytes(body) {\n\t\t\t\t\treturn sobek.Undefined()\n\t\t\t\t}","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/js/modules/k6/http/response.go#L52-L88","documentation":"Response.JSON() was called on a response whose Body is nil. The body is null because the request itself errored before a body was captured (network error, timeout, or thrown on status), leaving nothing to parse as JSON in the Sobek VM.","triggerScenarios":"Thrown at js/modules/k6/http/response.go:70 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Guard with res.Body != null (or check res.error) before calling res.json()","Resolve the underlying request failure so the response actually has a body"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"01ffac6f245854c1b8adc6a69857c76a15f90022","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}