antonmedv/fx · error

Unexpected character %q in object

Error message

Unexpected character %q in object

What it means

While iterating over object members, after a value the parser expects either ',' (next member) or '}' (close object). If the character is neither, it panics with "Unexpected character %q in object". This catches malformed separators and garbage between members.

Source

Thrown at internal/jsonx/json.go:391

				continue
			}
		}

		if p.char == '}' {
			closeBracket := &Node{
				Kind:       Object,
				Depth:      p.depth,
				LineNumber: p.lineNumberPlusPlus(),
			}
			closeBracket.Value = curlyBracketClose
			closeBracket.Parent = object
			closeBracket.Index = -1
			object.Append(closeBracket)
			p.next()
			return object
		}

		panic(fmt.Sprintf("Unexpected character %q in object", p.char))
	}
}

func (p *JsonParser) parseArray() *Node {
	arr := &Node{
		Kind:       Array,
		Depth:      p.depth,
		LineNumber: p.lineNumberPlusPlus(),
	}
	arr.Value = squareBracketOpen

	p.next()
	p.skipWhitespace()

	if p.char == ']' {
		arr.Value = squareBracketPair
		p.next()
		return arr

View on GitHub (pinned to 4f31cd3a0c)

Solutions

  1. Add the missing comma between members: {"a":1 "b":2} -> {"a":1, "b":2}
  2. Remove any stray characters after the value that are not ',' or '}'
  3. Re-run the document through a JSON formatter to identify the malformed spot
  4. Check for encoding/corruption issues (e.g. binary bytes in the file)

Example fix

// before
{"a": 1 "b": 2}

// after
{"a": 1, "b": 2}
Defensive patterns

Strategy: validation

Validate before calling

func validateArrayCommas(src string) error {
	// inside '[', after a value only ',' or ']' may appear; quick heuristic check
	inArr, inStr, esc, prev := 0, false, false, byte(0)
	for i := 0; i < len(src); i++ {
		c := src[i]
		if inStr { if esc { esc = false } else if c == '\\' { esc = true } else if c == '"' { inStr = false }; continue }
		switch c {
		case '"': inStr = true
		case '[': inArr++
		case ']': if inArr > 0 { inArr-- }
		}
		if inArr > 0 && (prev=='"'||isDigit(prev)) && isValueStart(c) && c != ',' {
			return fmt.Errorf("missing ',' between array elements at offset %d", i)
		}
		prev = c
	}
	return nil
}
func isDigit(c byte) bool { return c >= '0' && c <= '9' }
func isValueStart(c byte) bool { return isDigit(c) || c=='"' || c=='t' || c=='f' || c=='n' || c=='{' || c=='[' }

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 (bad array separator?): %v", r)
		}
	}()
	root = p.Parse(data)
	return
}

Prevention

When it happens

Trigger: Parsing {"a":1 "b":2} (missing comma), {"a":1; "b":2}, {"a":1 c}, or any stray character after a member value inside an object.

Common situations: Hand-edited JSON with a dropped comma between key/value pairs; copy-paste from JS object literals using ';' or newline-separated members; corrupted or truncated files with stray bytes.

Related errors


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