Parse JSON in Java using Google’s Gson and Jackson Library
This post will discuss how to parse JSON in Java using Google’s Gson and Jackson library.
Nowadays, almost all RESTful web services consume and produce JSON data instead of XML. Unfortunately, Java SE doesn’t support converting JSON to Java Object (and vice-versa). But there are many third-party libraries available that are very reliable and offer high performance.
To use Gson or Jackson library, we need to create POJO (Plain Old Java Object) with the same JSON structure. POJO is simply a class with private fields and public “getter” and “setter” methods. We recommend this online tool jsonschema2pojo for quickly generating POJO from json or json schema with Jackson/Gson annotation style.
1. Using Gson
Google’s Gson is one of the best libraries for parsing a JSON into POJO. It is very easy to learn and implement. We can use the Gson#fromJson() method to convert our JSON string into a Java object.
The following code decodes the given json string with the help of the Gson library:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
import com.google.gson.Gson; import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; import java.util.Arrays; import java.util.List; class Person { @SerializedName("name") @Expose private String name; @SerializedName("age") @Expose private Integer age; @SerializedName("student") @Expose private Student student; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } @Override public String toString() { return Arrays.asList(name, String.valueOf(age), student.toString()).toString(); } } class Student { @SerializedName("id") @Expose private String id; @SerializedName("subjects") @Expose private List<String> subjects = null; public String getId() { return id; } public void setId(String id) { this.id = id; } public List<String> getSubjects() { return subjects; } public void setSubjects(List<String> subjects) { this.subjects = subjects; } @Override public String toString() { return Arrays.asList(id, subjects.toString()).toString(); } } // Parse JSON using Google's Gson library class Main { public static void main(String[] args) { // JSON string String jsonString = "{\"name\":\"Jon Snow\",\"age\":22," + "\"student\":{\"id\":\"Jon_Snow_22\"," + "\"subjects\":[\"Maths\",\"Science\"]}}"; System.out.println("JSON string is: " + jsonString); // Parse JSON string to `Person` object using Google's Gson library Gson gson = new Gson(); Person person = gson.fromJson(jsonString, Person.class); System.out.println("Person object: " + person); } } |
Output:
JSON string is: {"name":"Jon Snow","age":22,"student":{"id":"Jon_Snow_22","subjects":["Maths","Science"]}}
Person object: [Jon Snow, 22, [Jon_Snow_22, [Maths, Science]]]
2. Using Jackson
Jackson is a multi-purpose, high-performance Java library for processing JSON. It provides Data Binding functionality that can be used for binding a JSON string into a POJO. All we need is to call the readValue() method of Jackson’s ObjectMapper class to convert our JSON string into the specified Java object.
Consider the following code, which parses the given json string using the Jackson library:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.annotate.*; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import java.io.IOException; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @JsonPropertyOrder({ "name", "age", "student" }) class Person { @JsonProperty("name") private String name; @JsonProperty("age") private Integer age; @JsonProperty("student") private Student student; @JsonIgnore private Map<String, Object> additionalProperties = new HashMap<>(); @JsonProperty("name") public String getName() { return name; } @JsonProperty("name") public void setName(String name) { this.name = name; } @JsonProperty("age") public Integer getAge() { return age; } @JsonProperty("age") public void setAge(Integer age) { this.age = age; } @JsonProperty("student") public Student getStudent() { return student; } @JsonProperty("student") public void setStudent(Student student) { this.student = student; } @JsonAnyGetter public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; } @JsonAnySetter public void setAdditionalProperty(String name, Object value) { this.additionalProperties.put(name, value); } @Override public String toString() { return Arrays.asList(name, String.valueOf(age), student.toString()) .toString(); } } @JsonPropertyOrder({ "id", "subjects" }) class Student { @JsonProperty("id") private String id; @JsonProperty("subjects") private List<String> subjects = null; @JsonIgnore private Map<String, Object> additionalProperties = new HashMap<>(); @JsonProperty("id") public String getId() { return id; } @JsonProperty("id") public void setId(String id) { this.id = id; } @JsonProperty("subjects") public List<String> getSubjects() { return subjects; } @JsonProperty("subjects") public void setSubjects(List<String> subjects) { this.subjects = subjects; } @JsonAnyGetter public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; } @JsonAnySetter public void setAdditionalProperty(String name, Object value) { this.additionalProperties.put(name, value); } @Override public String toString() { return Arrays.asList(id, subjects.toString()).toString(); } } // Parse JSON using the Jackson library class Main { public static void main(String[] args) { // JSON string String jsonString = "{\"name\":\"Jon Snow\",\"age\":22," + "\"student\":{\"id\":\"Jon_Snow_22\"," + "\"subjects\":[\"Maths\",\"Science\"]}}"; System.out.println("JSON string is: " + jsonString); ObjectMapper mapper = new ObjectMapper(); Person person = null; try { // Parse JSON string to `Person` object using the Jackson library person = mapper.readValue(jsonString, Person.class); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("Person object: " + person); } } |
Output:
JSON string is: {"name":"Jon Snow","age":22,"student":{"id":"Jon_Snow_22","subjects":["Maths","Science"]}}
Person object: [Jon Snow, 22, [Jon_Snow_22, [Maths, Science]]]
Useful JSON Tools:
That’s all about parsing JSON in Java using Google’s Gson and Jackson Library.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)