Unrecognized token: was expecting a value

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.

How the message appears

Jackson wording. The class is com.fasterxml.jackson.core.JsonParseException on Jackson 2.x, and Spring Boot normally wraps it in an HttpMessageNotReadableException before it reaches your logs.

  • com.fasterxml.jackson.core.JsonParseException: 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

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.

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.

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.

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.

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.

Fails to parse
{
  "id": 7,
  "status": ok
}
Parses
{
  "id": 7,
  "status": "ok"
}

The example travels in the URL fragment, which browsers never send to a server.

Worth knowing

  • Jackson 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.
  • Single-quoted values report a different message — Unexpected character (''' (code 39)) — because a quote is a character Jackson recognises but did not expect there.

Your JSON and text are processed entirely in your browser and are never sent anywhere. To understand how the site is used we record each page view — the page, the referrer and an approximate location from your IP address — and keep it for 90 days. Nothing is stored on your device unless you choose it. Accept to also store a device identifier here and record your browser and device characteristics, which is what lets a return visit be told from a new one. Decline and nothing is sent at all. We respect Do Not Track. See our privacy policy.