MalformedJsonException: Unterminated object
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.
{
"id": 1
"status": "shipped"
}Line 2: add ,.
{
"id": 1,
"status": "shipped"
}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.
- 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
Unterminated object: a comma is missing
Gson read
"id": 1and 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.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 saysExpected name. The path is the last key it read,$.statushere. Remove the comma.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 anEOFExceptionand not aMalformedJsonException. Gson wraps both in aJsonSyntaxException, so that is the one to catch.Use JsonReader.setStrictness: the text is not JSON
An HTML error page read into a plain
Objectgives 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.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 doesJSON.parsein 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 givesExpected 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.