Unrecognized token: was expecting a value

ErrorJava

com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'ok': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')

Where JSON.parse stops

  "id": 7,
  "status": ok
}

What it means

Jackson lists the seven things a JSON value can be, because it found something that was none of them: a bare run of letters. The token it quotes back at you is the exact text it could not classify, which usually makes the cause obvious.

Last verified on Jackson 2.21 and 3.1; Spring 6.2 and 7.0.

It opens with the error marked. Nothing is uploaded. Find where the error is.

Usually
An unquoted string valueundefined, from a JavaScript producerThe body is not JSON at allA truncated keyword
Fix
Put double quotes around the value.

The fix

Put double quotes around the value. If the token Jackson quotes back is undefined, fix the producer rather than the document: JSON has no such value, and null or an absent key is what was meant.

broken.jsonFails to parse
{
  "id": 7,
  "status": ok
}
JSON.parse stops at line 3, column 13.

Line 3: add " twice.

fixed.jsonParses
{
  "id": 7,
  "status": "ok"
}
JSON.parse reads it.

How the message appears

Jackson 2 first, then Jackson 3, then what Spring shows. The class is com.fasterxml.jackson.core.JsonParseException on Jackson 2 and tools.jackson.core.exc.StreamReadException on Jackson 3, which Spring 7 uses. Each is followed by its location line. Spring wraps either one in an HttpMessageNotReadableException, with the same wording on Spring 6.2.15 and 7.0.1, before it reaches your logs.

Console
  • com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'ok': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
  • at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 3, column: 13]
  • tools.jackson.core.exc.StreamReadException: Unrecognized token 'ok': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
  • at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); byte offset: #UNKNOWN]
  • org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unrecognized token 'ok': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
  • Unrecognized token 'undefined': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')

What causes it

  1. An unquoted string value

    Values that look like keywords get written without quotes constantly: ok, yes, active, N/A. JSON has no bare words apart from true, false and null, so every one of them has to be quoted.

  2. undefined, from a JavaScript producer

    JavaScript has undefined and JSON does not. A serialiser that writes it out produces a document that no strict parser will read: the fix belongs on the producing side, where undefined should become null or be omitted.

  3. The body is not JSON at all

    A plain-text error such as error: upstream timeout reports Unrecognized token 'error'. Check the status code and content type before parsing, rather than trying to fix the document.

  4. A truncated keyword

    Unrecognized token 'nul' means the document was cut off mid-word: a truncated response or a buffer written short, not a typo.

Worth knowing

Watch outJackson can be told to accept some of this: JsonReadFeature.ALLOW_UNQUOTED_FIELD_NAMES, ALLOW_SINGLE_QUOTES, ALLOW_TRAILING_COMMA. Enabling them makes your service accept documents that every other consumer downstream will reject, so it is worth being deliberate about.

NoteSingle-quoted values report a different message (Unexpected character (''' (code 39))) because a quote is a character Jackson recognises but did not expect there.

What you put into the tools never leaves your browser. We count page views and keep them for 90 days. Accept and we also remember this browser, so a return visit is not counted as new. Privacy policy