vitessio/vitess · error

expected ',' or '}' in object, got %q

Error message

expected ',' or '}' in object, got %q

What it means

While writing a JSON object to SQL form, writeObject expects each member after the first to be separated by a comma and the object eventually closed with '}'. If, after writing a key/value pair, the next non-whitespace byte is neither ',' nor '}', the writer aborts with this error. It indicates malformed object syntax — a missing separator, a stray token, or a truncated object.

Source

Thrown at go/mysql/json/marshal.go:276

	}
}

func (w *sqlWriter) writeObject(depth int) error {
	w.buf.WriteString("JSON_OBJECT(")
	first := true
	for {
		w.skipWhitespace()
		if w.pos >= len(w.data) {
			return errors.New("unexpected end of JSON input in object")
		}
		if w.data[w.pos] == '}' {
			w.pos++
			w.buf.WriteByte(')')
			return nil
		}
		if !first {
			if w.data[w.pos] != ',' {
				return fmt.Errorf("expected ',' or '}' in object, got %q", w.data[w.pos])
			}
			w.pos++
			w.buf.WriteString(", ")
			w.skipWhitespace()
		}
		first = false

		// Key (always a string).
		if w.pos >= len(w.data) || w.data[w.pos] != '"' {
			return errors.New("expected string key in JSON object")
		}
		w.buf.WriteString("_utf8mb4")
		if err := w.writeStringContent(); err != nil {
			return fmt.Errorf("reading JSON object key: %w", err)
		}
		w.buf.WriteString(", ")

		// Colon separator.

View on GitHub (pinned to 01a25a7d17)

Solutions

  1. Fix the JSON so object members are separated by commas: {"a": 1, "b": 2}
  2. Validate the input with encoding/json.Valid before marshaling to catch the malformation early
  3. Check template/conditional serialization logic that may omit the separator between fields
  4. Inspect for truncated input — if the document is cut off, fix the producer rather than patching the string

Example fix

// before
raw := []byte(`{"a": 1 "b": 2}`)
out, err := json.AppendMarshalSQL(nil, raw)
// after
raw := []byte(`{"a": 1, "b": 2}`)
out, err := json.AppendMarshalSQL(nil, raw)
Defensive patterns

Strategy: validation

Validate before calling

raw := []byte(input)
if !json.Valid(raw) {
    return fmt.Errorf("input is not valid JSON: missing separators or truncated object")
}

Try / catch

out, err := json.AppendMarshalSQL(nil, raw)
if err != nil {
    if strings.Contains(err.Error(), "expected ',' or '}'") {
        return fmt.Errorf("malformed JSON object near: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: AppendMarshalSQL on input where an object member is followed by something other than ',' or '}' — e.g. `{"a": 1 "b": 2}` (missing comma), `{"a": 1; }` (wrong separator), or truncated input like `{"a": 1` followed by end of data hitting an unexpected byte.

Common situations: Hand-built JSON strings missing commas between fields; template-generated JSON with conditional fields that drop separators; log/trace payloads truncated mid-object; mixing serialization formats (e.g. semicolons).

Related errors


AI-assisted analysis of vitessio/vitess@01a25a7d17 (2026-09-01). Data as JSON: /api/errors/baa12824f0197035. Report an issue: GitHub.