grafana/k6 · error

couldn't parse the configuration from %q: %w

Error message

couldn't parse the configuration from %q: %w

What it means

Emitted by readDiskConfig when the on-disk config file was read successfully but json.Unmarshal failed to decode it as JSON. Any syntax error — trailing commas, single quotes, comments, truncated files, BOM-prefixed files — produces this error wrapping the encoding/json error (e.g. 'invalid character ... looking for beginning of value').

Source

Thrown at internal/cmd/config.go:153

func readDiskConfig(gs *state.GlobalState) (Config, error) {
	// Try to see if the file exists in the supplied filesystem
	if _, err := gs.FS.Stat(gs.Flags.ConfigFilePath); err != nil {
		if errors.Is(err, fs.ErrNotExist) && gs.Flags.ConfigFilePath == gs.DefaultFlags.ConfigFilePath {
			// If the file doesn't exist, but it was the default config file (i.e. the user
			// didn't specify anything), silence the error
			err = nil
		}
		return Config{}, err
	}

	data, err := fsext.ReadFile(gs.FS, gs.Flags.ConfigFilePath)
	if err != nil {
		return Config{}, fmt.Errorf("couldn't load the configuration from %q: %w", gs.Flags.ConfigFilePath, err)
	}
	var conf Config
	err = json.Unmarshal(data, &conf)
	if err != nil {
		return Config{}, fmt.Errorf("couldn't parse the configuration from %q: %w", gs.Flags.ConfigFilePath, err)
	}
	return conf, nil
}

// Permissions for the on-disk config file and its containing directory.
// The config can contain the Grafana Cloud API token (collectors.cloud.token),
// so it must not be readable by other local users.
const (
	configFileMode = fs.FileMode(0o600)
	configDirMode  = fs.FileMode(0o700)
)

// writeDiskConfig serializes the configuration to a JSON file and writes it in the supplied
// location on the supplied filesystem.
//
// The file may contain the Grafana Cloud API token, so it is written with
// owner-only permissions (0o600), inside a directory created with owner-only
// permissions (0o700). If the file or directory already exists with looser

View on GitHub (pinned to 93accf6570)

Solutions

  1. Validate the exact file from the error message with a JSON linter: `jq . <path>` — jq pinpoints the offending offset
  2. Fix the reported character/position (remove trailing commas, use double quotes, drop comments/BOM)
  3. Keep the config minimal and prefer environment variables (K6_* env config) where JSON pitfalls recur
  4. If unsure, move the file away and let k6 regenerate/ignore it, then re-add needed keys one at a time

Example fix

// before (~/.config/loadimpact/k6.json)
{ "collectors": { "cloud": { "token": "abc", } } }  // trailing comma -> couldn't parse the configuration

// after
{ "collectors": { "cloud": { "token": "abc" } } }
Defensive patterns

Strategy: validation

Validate before calling

# Fail fast on malformed config JSON before any k6 call
cfg="${K6_CONFIG:-$HOME/.config/loadimpact/k6.json}"
[ ! -f "$cfg" ] || jq -e . "$cfg" >/dev/null || { echo "invalid JSON in $cfg"; exit 1; }

Type guard

// Go callers embedding k6 can pre-validate:
func validK6Config(data []byte) bool { var v map[string]any; return json.Unmarshal(data, &v) == nil }

Prevention

When it happens

Trigger: Hand-editing ~/.config/loadimpact/k6.json and leaving invalid JSON; writing YAML into the JSON config file; a truncated file from an interrupted write; a UTF-8 BOM emitted by a Windows editor; trailing comma after the last key.

Common situations: Adding a token or default options to the config file manually; converting between k6's JSON config and script YAML options; editors auto-adding commas or smart quotes; CI images baked with a malformed config.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/bfeaf42e6c7dc2fc. Report an issue: GitHub.