apple/pkl · error · ParseException

',' or ']'

Error message

',' or ']'

What it means

The JSON parser throws expected("',' or ']'") when, inside an array, after a value it encounters neither a `,` (next element) nor `]` (array end). Typically caused by a missing comma between elements, an unquoted string, or a stray character inside `[...]`. Surfaces as the hint of jsonParseError from `json.parse`.

Source

Thrown at pkl-core/src/main/java/org/pkl/core/util/json/JsonParser.java:163

    read();
    if (++nestingLevel > MAX_NESTING_LEVEL) {
      throw error("Nesting too deep");
    }
    skipWhiteSpace();
    if (readChar(']')) {
      nestingLevel--;
      handler.endArray(array);
      return;
    }
    do {
      skipWhiteSpace();
      handler.startArrayValue(array);
      readValue();
      handler.endArrayValue(array);
      skipWhiteSpace();
    } while (readChar(','));
    if (!readChar(']')) {
      throw expected("',' or ']'");
    }
    nestingLevel--;
    handler.endArray(array);
  }

  private void readObject() throws IOException {
    var object = handler.startObject();
    read();
    if (++nestingLevel > MAX_NESTING_LEVEL) {
      throw error("Nesting too deep");
    }
    skipWhiteSpace();
    if (readChar('}')) {
      nestingLevel--;
      handler.endObject(object);
      return;
    }
    do {

View on GitHub (pinned to f3efcbfc9b)

Solutions

  1. Insert the missing comma between array elements at the position from the hint
  2. Quote any unquoted strings in the array
  3. Remove stray characters (extra identifiers, comments) inside the array
  4. Validate the JSON with a linter to pinpoint the exact offending position

Example fix

// before
v = json.parse("[1 2, 3]")
// after
v = json.parse("[1, 2, 3]")
Defensive patterns

Strategy: try-catch

Validate before calling

// quote balance sanity check inside arrays
inStr = false; text.chars.forEach((c) -> {
  if (c == '"') inStr = !inStr
})
require(!inStr, "unbalanced quotes in JSON input")

Try / catch

try {
  value = json.parse(text)
} catch (e) {
  trace("JSON syntax error: ${e.message}")  // hint points at ',' or ']' expectation
  throw e
}

Prevention

When it happens

Trigger: Input like `[1 2]`, `["a" "b"]`, `[1, 2 ,]`-style mistakes resolved wrong, or `[1, 2 x]`. Raised in JsonParser.readArray when readChar(']') fails after the element loop.

Common situations: Hand-written JSON with missing commas; values containing raw quotes or characters that terminate strings early; copy-paste from non-JSON sources (JS arrays without quotes); template artifacts inside arrays.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08). Data as JSON: /api/errors/6934063dab0966f0. Report an issue: GitHub.