can1357/oh-my-pi · error · SyntaxError

Expected ',' or ']' in array

Error message

Expected ',' or ']' in array

What it means

Thrown by RelaxedJson.#array in strict mode after an element when the next character is neither ',' nor ']' — the array elements are not properly delimited. Partial mode returns the elements parsed so far instead of throwing.

Source

Thrown at packages/utils/src/json-parse.ts:335

			if (c === ",") {
				this.#i++;
				continue;
			}
			const value = this.#value(true);
			if (value === INCOMPLETE) return out;
			out.push(value);
			this.#ws();
			const d = this.#i < this.#n ? this.#s[this.#i] : "";
			if (d === ",") {
				this.#i++;
				continue;
			}
			if (d === "]") {
				this.#i++;
				return out;
			}
			if (this.#partial) return out;
			throw new SyntaxError("Expected ',' or ']' in array");
		}
	}

	#key(): string {
		const c = this.#s[this.#i];
		if (c === '"' || c === "'") return this.#string(this.#s.charCodeAt(this.#i));
		// Unquoted identifier key: read until a structural delimiter / whitespace.
		const start = this.#i;
		while (this.#i < this.#n) {
			const ch = this.#s[this.#i];
			if (ch === ":" || ch === "," || ch === "}" || isWhitespace(this.#s.charCodeAt(this.#i))) break;
			this.#i++;
		}
		if (this.#i === start) {
			if (this.#partial) return "";
			throw new SyntaxError("Expected object key");
		}
		return this.#s.slice(start, this.#i);

View on GitHub (pinned to 9690622007)

Solutions

  1. Insert the missing ',' or ']' at the failing position in the source
  2. Use parseStreamingJson if the input may be an incomplete prefix
  3. Validate the producer's serialization (e.g. always JSON.stringify arrays, never hand-build them) so delimiters cannot be dropped

Example fix

// before
const v = parseJsonWithRepair(`[1 2]`); // SyntaxError
// after
const v = parseJsonWithRepair(`[1, 2]`); // [1, 2]
Defensive patterns

Strategy: validation

Validate before calling

// Delimiter check between array elements before parsing
const stripped = text.replace(/"(\\.|[^"\\])*"/g, '"S"');
const hasBadDelimiter = /\S\s+\S/.test(stripped.replace(/[\[\],\s0-9.eE+\-]/g, ""));

Try / catch

try {
  const value = parseJsonWithRepair<T>(text);
} catch (err) {
  if (err instanceof SyntaxError && err.message === "Expected ',' or ']' in array") {
    // fix the delimiter or reject/regenerate the payload
  } else throw err;
}

Prevention

When it happens

Trigger: parseJsonWithRepair with '[1 2]', '["a" "b"]', or any non-','/']' character between elements in strict mode.

Common situations: An LLM emitting space-separated array items without commas; a formatting bug that dropped the delimiter when serializing; copy/paste merging two lines of an array literal.

Related errors


AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31). Data as JSON: /api/errors/7b339a4f9b302d0c. Report an issue: GitHub.