How to Parse JSON in Java
In this tutorial we will look at how to parse JSON in Java using different libraries.
JSON stands for JavaScript Object Notation, and it is based on a subset of JavaScript.
As a data-exchange format, it is widely used in web programming. Here we show how to parse JSON in Java using the org.json
library.
A JSON object is an unordered set of key/value pairs. A JSON array is an ordered collection of values. The values themselves could be objects or arrays.
We will be parsing this JSON as an example to retrieve values for pageName
, pagePic
and post_id
{
"pageInfo": {
"pageName": "Homepage",
"logo": "https://www.example.com/logo.jpg"
},
"posts": [
{
"post_id": "0123456789",
"actor_id": "1001",
"author_name": "Jane Doe",
"post_title": "How to parse JSON in Java",
"comments": [],
"time_of_post": "1234567890"
}
]
}
Parse JSON Using org.json
To use org.json to parse JSON in Java, you need to add the library as a dependency. This can be fetched from Maven repository
import org.json.JSONArray;
import org.json.JSONObject;
public class ParseJSON {
static String json = "...";
public static void main(String[] args) {
JSONObject obj = new JSONObject(json);
String pageName = obj.getJSONObject("pageInfo").getString("pageName");
System.out.println(pageName);
JSONArray arr = obj.getJSONArray("posts");
for (int i = 0; i < arr.length(); i++) {
String post_id = arr.getJSONObject(i).getString("post_id");
System.out.println(post_id);
}
}
}
N.B. the ...
needs to be replaced by the JSON string. This has been omitted from the code above for clarity.
First, we need to convert the JSON string into a JSON Object, using JSONObject class.
Also, note that “pageInfo” is a JSON Object, so we use the getJSONObject method.
Likewise, “posts” is a JSON Array, so we need to use the getJSONArray method.
Parse JSON Using Gson
In order to use Gson to parse JSON in Java, you need to add the library as a dependency. You can get the latest version from Maven repository
The below example shows how to Parse the above JSON with Gson.
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class ParseJSON {
static String json = "...";
public static void main(String[] args) {
JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
String pageName = jsonObject.getAsJsonObject("pageInfo").get("pageName").getAsString();
System.out.println(pageName);
JsonArray arr = jsonObject.getAsJsonArray("posts");
for (int i = 0; i < arr.size(); i++) {
String post_id = arr.get(i).getAsJsonObject().get("post_id").getAsString();
System.out.println(post_id);
}
}
}
Like the previous example, the ...
needs to be replaced by the JSON string.
Parse JSON Using JsonPATH
The above two examples require a full deserialization of the JSON into a Java object before accessing the value in the property of interest. Another alternative, which does not go this route is to use JsonPATH which is like XPath for JSON and allows traversing of JSON objects.
Like before, you need to add JsonPATH as a dependency, which can be fetched from Maven repository
For example, to parse the above JSON we can use:
import com.jayway.jsonpath.JsonPath;
public class ParseJSON {
static String json = "...";
public static void main(String[] args) {
String pageName = JsonPath.read(json, "$.pageInfo.pageName");
System.out.println(pageName);
Integer posts = JsonPath.read(json, "$.posts.length()");
for(int i=0; i < posts; i++) {
String post_id = JsonPath.read(json, "$.posts[" + i + "].post_id");
System.out.println(post_id);
}
}
}