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

  1. Insert the missing comma between elements: [1 2] -> [1, 2]
  2. Remove stray characters that are neither ',' nor ']'
  3. Validate the document with a JSON linter to pinpoint the character
  4. 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

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

Related errors


AI-assisted analysis of antonmedv/fx@4f31cd3a0c (2026-09-02). Data as JSON: /api/errors/ae695eb74140d6ad. Report an issue: GitHub.