cilium/cilium · error
could not transcode %q value: %w
Error message
could not transcode %q value: %w
What it means
Before writing, the kvstore 'set' command runs tryTranscodeFromJSON on the trimmed file content; if transcoding fails (e.g. the key has a registered schema/type and the file content is not valid or compatible JSON) it wraps the error as 'could not transcode %q value: %w'.
Source
Thrown at pkg/kvstore/commands.go:57
Args: "key value-file",
},
func(s *script.State, args ...string) (script.WaitFunc, error) {
if len(args) != 2 {
return nil, fmt.Errorf("%w: expected key and value file", script.ErrUsage)
}
key := args[0]
value, err := os.ReadFile(s.Path(args[1]))
if err != nil {
return nil, fmt.Errorf("could not read %q: %w", s.Path(args[1]), err)
}
// As this is a dev/test only command, we can be a bit more
// aggressive with trimming whitespace to simplify our test scripts.
value = bytes.TrimSpace(value)
value, err = tryTranscodeFromJSON(key, value)
if err != nil {
return nil, fmt.Errorf("could not transcode %q value: %w", key, err)
}
return nil, c.client.Update(s.Context(), key, value, false)
},
)
}
func (c cmds) delete() script.Cmd {
return script.Command(
script.CmdUsage{
Summary: "delete kvstore key-value",
Args: "key",
},
func(s *script.State, args ...string) (script.WaitFunc, error) {
if len(args) != 1 {
return nil, fmt.Errorf("%w: expected key", script.ErrUsage)
}
return nil, c.client.Delete(s.Context(), args[0])View on GitHub (pinned to ac7b90affa)
Solutions
- Validate the value file contains well-formed JSON (jq . value.txt)
- Trim stray whitespace/newlines (the command already trims spaces but content must still be valid JSON)
- Check whether the key has a registered transcoding and what type it expects
Example fix
// before
// value.txt: {"a": 1,}
// after
// value.txt: {"a": 1} Defensive patterns
Strategy: validation
Validate before calling
if _, err := json.Valid(value); !err {
return fmt.Errorf("value for %q is not valid JSON", key)
} Prevention
- Validate JSON in value files with jq before use
- Know which key prefixes are JSON-transcoded
- Avoid stray characters; keep value files minimal and trimmed
When it happens
Trigger: tryTranscodeFromJSON(key, value) returns an error — the value file content is not valid JSON when the key requires JSON decoding, or doesn't match the expected shape for that key.
Common situations: Malformed JSON in the value file (trailing commas, unquoted strings); whitespace/newlines breaking parsing for keys expecting strict JSON; writing a JSON value to a key registered for a different type.
Understand the failure class
Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.
Related errors
- %w: expected key and value file
- could not read %q: %w
- %w: expected key
- error listing %q: %w
- remote cluster configuration required but not found
AI-assisted analysis of cilium/cilium@ac7b90affa (2026-08-31).
Data as JSON: /api/errors/9a5c3611d4030bae.
Report an issue: GitHub.