JSON is the most used data exchange format nowadays, and as a Java developer you should know about it. In this article, I will show you several ways to parse JSON in Java the simple and easy way. It’s practical and you can apply in almost any platform that use Java language.
Before starting, I’d like to share that there are many Java JSON libraries created to handle parsing JSON data structures like Gson, Genson, org.json, Jackson… But the best ones are these two: Gson and Jackson.
We will continue with examples of using Gson and Jackson to parse JSON in Java.
First let say we have the following JSON data:
{
"title": "Simple guide to parse JSON in Java",
"author": "Pete Houston",
"url": "https://blog.petehouston.com",
"tags": ["json", "java", "gson", "jackson"]
}
Article Contents
Using Gson
Since Gson is a third-party library, we need to import it into project first. If you’re using Maven, add Gson dependency as following:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
with Gradle, it would be like this:
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.2'
To parse it into Object using Gson, we create a class to represent this JSON data structure, let say, it is Article
class Article {
private String title;
private String author;
private String url;
private List tags;
public Article() {
}
public Article(String title, String author, String url, List tags) {
this.title = title;
this.author = author;
this.url = url;
this.tags = tags;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public List getTags() {
return tags;
}
@Override
public String toString() {
return "Article{" +
"title='" + title + '\'' +
", author='" + author + '\'' +
", url='" + url + '\'' +
", tags=" + String.join(", ", tags) +
'}';
}
}
Everything is setup, we just have to instantiate a new Gson object and use fromJson
method to transform the JSON string into an Article
object.
String jsonArticle = "{" +
"\"title\": \"Simple guide to parse JSON in Java\"," +
"\"author\": \"Pete Houston\"," +
"\"url\": \"https://blog.petehouston.com\"," +
"\"tags\": [\"json\", \"java\", \"gson\", \"jackson\"]" +
"}";
Gson gson = new Gson();
Article article = gson.fromJson(jsonArticle, Article.class);
Using Jackson
To use Jackson, we need to import jackson-databind module into project.
via Maven
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
via Gradle
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.5'
To parse using Jackson API, we need to instantiate an ObjectMapper
object, then use it to convert JSON string into object model using readValue
method.
ObjectMapper mapper = new ObjectMapper();
Article article = null;
try {
article = mapper.readValue(jsonArticle, Article.class);
} catch (IOException e) {
e.printStackTrace();
}
Compare Gson and Jackson to parse JSON in Java
There is a slightly difference when using Jackson API with Gson is that, you need to provide a default constructor to the object model; otherwise, it will throw an exception failing to convert object model without default constructor.
Another different point is that Jackson API enforces developers to catch exception to handle abnormal cases, such as empty input, invalid JSON format, … whilst Gson doesn’t require developers to handle any exception. Be aware that Gson still throws exception on invalid input.
So the good practice is to always catch exception when using either one.
Conclusion
As you see, by using third-party libraries like Gson, or Jackson, we can parse JSON very easily, not that much difficult. Also, there is one reason we should use these two libraries that they are well-created and well-optimized for handling JSON being used by many big companies in various large projects. So you don’t have to worry about using them.