grafana/k6 · error

the body is null so we can't transform it to JSON - this lik

Error message

the body is null so we can't transform it to JSON - this likely was because of a request error getting the response

What it means

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.

Source

Thrown at js/modules/k6/http/response.go:70

	}

	sel, err := html.ParseHTML(rt, body)
	if err != nil {
		common.Throw(rt, err)
	}
	sel.URL = res.URL
	if len(selector) > 0 {
		sel = sel.Find(selector[0])
	}
	return sel
}

// JSON parses the body of a response as JSON and returns it to the Sobek VM.
func (res *Response) JSON(selector ...string) sobek.Value {
	rt := res.client.moduleInstance.vu.Runtime()

	if res.Body == nil {
		err := fmt.Errorf("the body is null so we can't transform it to JSON" +
			" - this likely was because of a request error getting the response")
		common.Throw(rt, err)
	}

	hasSelector := len(selector) > 0
	if res.cachedJSON == nil || hasSelector { //nolint:nestif
		var v any

		body, err := common.ToBytes(res.Body)
		if err != nil {
			common.Throw(rt, err)
		}

		if hasSelector {
			if !res.validatedJSON {
				if !gjson.ValidBytes(body) {
					return sobek.Undefined()
				}

View on GitHub (pinned to 01ffac6f24)

Solutions

  1. Guard with res.Body != null (or check res.error) before calling res.json()
  2. Resolve the underlying request failure so the response actually has a body
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at js/modules/k6/http/response.go:70 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18). Data as JSON: /api/errors/ccf0705c0be53d22. Report an issue: GitHub.