Gson – MalformedJSONException fix example shows what is MalformedJSONException and how to fix it while using the Gson library in Java. This example also shows how to solve the MalformedJSONException using various methods.
What is MalformedJSONException and when is it thrown?
As the name suggests, the MalformedJSONException occurs when we try to read/parse JSON data that is invalid or not in the proper format. The problem could be because the JSON objects and/or JSON arrays are not opened or closed properly, presence of the invalid characters, unterminated string values, or any other formatting issues.
When Gson encounters any such invalid JSON data, it throws com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException while reading/parsing it.
The below given JSON is not valid. The value of the “name” property is not terminated properly due to missing double quotes.
1 2 3 4 5 |
{ "id": 1, "name": "Bob, "subjects": ["Maths", "Chemistry"] } |
When we try to parse this JSON using the Gson, it will throw com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.javacodeexamples.gsonexamples; import com.google.gson.Gson; import com.google.gson.JsonObject; public class GsonMalformedJsonException { public static void main(String[] args) { String jsonData = "{\"id\": 1,\"name\": \"Bob,\"subjects\": [\"Maths\", \"Chemistry\"]}"; Gson gson = new Gson(); JsonObject jsonObject = gson.fromJson(jsonData, JsonObject.class); System.out.println(jsonObject); } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Exception in thread "main" com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 25 path $.name at com.google.gson.Gson.fromJson(Gson.java:1241) at com.google.gson.Gson.fromJson(Gson.java:1137) at com.google.gson.Gson.fromJson(Gson.java:1047) at com.google.gson.Gson.fromJson(Gson.java:982) at com.javacodeexamples.gsonexamples.GsonMalformedJsonException.main(GsonMalformedJsonException.java:13) Caused by: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 25 path $.name at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1659) at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:500) at com.google.gson.stream.JsonReader.hasNext(JsonReader.java:422) at com.google.gson.internal.bind.TypeAdapters$28.read(TypeAdapters.java:779) at com.google.gson.internal.bind.TypeAdapters$28.read(TypeAdapters.java:725) at com.google.gson.internal.bind.TypeAdapters$34$1.read(TypeAdapters.java:1007) at com.google.gson.Gson.fromJson(Gson.java:1227) ... 4 more |
How to solve MalformedJSONException?
Solution 1:
Make sure that the input JSON is in a valid format. We can use a service like https://jsonlint.com to validate the JSON. If it is invalid, fix the issue in the input JSON to fix the MalformedJsonException.
Solution 2:
Sometimes the input source adds extra characters at the end of the JSON object that are not whitespace characters. The JSON only considers ‘\n’, ‘r’, and ‘\t’ as whitespace characters. Any characters other than these after the end of the JSON object, for example, a null character ‘\0’, will cause the MalformedJsonException.
A simple fix to remove these characters from the input JSON is to use the trim method.
1 2 3 4 5 6 7 8 |
String jsonData = "{\"id\": 1,\"name\": \"Bob\",\"subjects\": [\"Maths\", \"Chemistry\"]}"; Gson gson = new Gson(); //trim the input JSON JsonObject jsonObject = gson.fromJson(jsonData.trim(), JsonObject.class); System.out.println(jsonObject); |
Solution 3:
If the first two solutions do not work, we can set the JSON parsing in a lenient mode that allows JSON data that does not strictly comply with the JSON specification.
1 2 3 4 5 6 7 8 9 10 11 |
String jsonData = "{\"id\": 1,\"name\": \"Bob\",\"subjects\": [\"Maths\", \"Chemistry\"]}"; /* * Create Gson object using the GsonBuilder, * and set the Lenient mode */ Gson gson = new GsonBuilder().setLenient().create(); JsonObject jsonObject = gson.fromJson(jsonData, JsonObject.class); System.out.println(jsonObject); |
Please let me know your views in the comments section below.