MalformedJsonException: Unterminated object

ErrorJava

com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 3 column 4 path $.id

Where JSON.parse stops

  "id": 1
  "status": "shipped"
}

What it means

Gson stopped because the text is not valid JSON. The message names the problem and the path of the last key it read. In the example the comma after 1 is missing, so the path is $.id and the mistake is right after it.

Last verified on Gson 2.13.

The validator opens with the error marked. Examples travel in the URL fragment, which browsers never send to a server.

Express Lint lays broken JSON out by bracket depth and marks the line where it stops. It changes only spaces and line breaks outside strings. Strings in either quote stay as written. If the quotes do not pair up, it leaves your text as it is. Ctrl+Z undoes it. See how it works.

Usually
Unterminated object: a comma is missingExpected name: a comma before the closing braceEnd of input: the text stops earlyUse JsonReader.setStrictness: the text is not JSONGson accepts some bad JSON
Fix
Find the spot from the path, then fix the text.

The fix

Find the spot from the path, then fix the text. In the example, add the comma after 1. If the message does not make the spot clear, paste the document into the validator.

broken.jsonFails to parse
{
  "id": 1
  "status": "shipped"
}
JSON.parse stops at line 3, column 3.

Line 2: add ,.

fixed.jsonParses
{
  "id": 1,
  "status": "shipped"
}
JSON.parse reads it.

How the message appears

The first line is the example on this page, read with the default Gson. Then a comma before a closing brace, a document that stops early, and an HTML page. The last line is the link Gson adds to the malformed ones. The End of input line does not have it.

Console
  • com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 3 column 4 path $.id
  • com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected name at line 1 column 26 path $.status
  • com.google.gson.JsonSyntaxException: java.io.EOFException: End of input at line 1 column 30 path $.status
  • com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed JSON at line 1 column 18 path $
  • See https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json

What causes it

  1. Unterminated object: a comma is missing

    Gson read "id": 1 and expected a comma or the closing }. It found the next key instead. The path is $.id, the last key it read, so the mistake is right after that value. Add the comma.

  2. Expected name: a comma before the closing brace

    A comma after the last member says another key is coming. Gson reads the } where the key should be and says Expected name. The path is the last key it read, $.status here. Remove the comma.

  3. End of input: the text stops early

    A body that is cut off in the middle of an object ends with End of input. That one is an EOFException and not a MalformedJsonException. Gson wraps both in a JsonSyntaxException, so that is the one to catch.

  4. Use JsonReader.setStrictness: the text is not JSON

    An HTML error page read into a plain Object gives this message. It tells you to turn on lenient mode. Do not. The text is not JSON, so find out why you got a web page. If you read the same page into your own class, you get Expected BEGIN_OBJECT but was STRING instead.

  5. Gson accepts some bad JSON

    The default Gson reads single quotes, as in {'id': 1}, and a comma after the last item of an array, as in [1,], with no error. A strict Gson reader rejects both, and so does JSON.parse in a browser. So a document can pass in your Gson code and still break somewhere else. A comma after the last member of an object is not accepted. That gives Expected name.

Worth knowing

NoteJackson and Spring word the same missing comma differently. See the Spring page.

NoteJavaScript's version is Expected ',' or '}' after property value.

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.