antonmedv/fx · error
Invalid character %q in array
Error message
Invalid character %q in array
What it means
While iterating over array elements, after each value the parser expects ',' or ']'. Any other character triggers this panic with the offending character. It enforces valid element separation in JSON arrays.
Source
Thrown at internal/jsonx/json.go:453
continue
}
}
if p.char == ']' {
closeBracket := &Node{
Kind: Array,
Depth: p.depth,
LineNumber: p.lineNumberPlusPlus(),
}
closeBracket.Value = squareBracketClose
closeBracket.Parent = arr
closeBracket.Index = -1
arr.Append(closeBracket)
p.next()
return arr
}
panic(fmt.Sprintf("Invalid character %q in array", p.char))
}
}
func (p *JsonParser) parseKeyword(name string, kind Kind) *Node {
start := p.end - 1
for i := 1; i < len(name); i++ {
p.next()
if p.char != name[i] {
panic(fmt.Sprintf("Unexpected character %q in keyword", p.char))
}
}
p.next()
if isEndOfValue(p.char) {
keyword := &Node{
Kind: kind,
Depth: p.depth,
Value: string(p.data[start : p.end-1]),
LineNumber: p.lineNumberPlusPlus(),View on GitHub (pinned to 4f31cd3a0c)
Solutions
- Insert the missing comma between elements: [1 2] -> [1, 2]
- Remove stray characters that are neither ',' nor ']'
- Validate the document with a JSON linter to pinpoint the character
- Check numbers for embedded extra dots/characters (e.g. [1.2.3] -> [1.2, 3])
Example fix
// before [1, 2 3] // after [1, 2, 3]
Defensive patterns
Strategy: validation
Validate before calling
func preflightJSON(src []byte) error {
// cheap structural sanity: run stdlib unmarshal into interface{} first
var v interface{}
if err := json.Unmarshal(src, &v); err != nil {
return fmt.Errorf("input is not valid JSON: %w", err)
}
return nil
}
// call before handing data to the custom parser:
// if err := preflightJSON(data); err != nil { skip / report } Try / catch
func safeParse(p *jsonx.JsonParser, data []byte) (root *jsonx.Node, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("json parse failed in array: %v", r)
}
}()
root = p.Parse(data)
return
} Prevention
- Validate input with encoding/json (or jq) before custom parsing
- Check generated numeric strings for stray dots/characters before joining into arrays
- Use serializers to build arrays; avoid manual string assembly
- Log the surrounding input bytes on failure to spot corruption early
When it happens
Trigger: Parsing [1 2] (missing comma), [1; 2], [1 x], or stray characters after the last element (e.g. [1]2 producing an earlier different error, [1 ,2] fine but [1 ..2] panics here).
Common situations: Hand-written arrays with missing commas; copy-paste from code using tuple-style syntax; corrupted numeric input like [1.2.3] where the second '.' lands between elements.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- Expected object key to be a string, got %q
- Expected colon after object key, got %q
- Unexpected character %q in object
- Invalid escape sequence '\%c'
- Invalid character %q in number
AI-assisted analysis of antonmedv/fx@4f31cd3a0c (2026-09-02).
Data as JSON: /api/errors/ae695eb74140d6ad.
Report an issue: GitHub.