How to parse JSON in Java

JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write. It is widely used for transmitting data between a server and a web application, as an alternative to XML.

Why Parse JSON in Java?

In Java, parsing JSON is necessary in many scenarios. For example, when you receive JSON data from a web service or when you want to convert JSON data into Java objects for further processing. Java provides several libraries and APIs to parse JSON easily.

JSON Parsing in Java - Step by Step

Let's dive into the step-by-step process of parsing JSON in Java.

Step 1: Importing Libraries

Before we begin, we need to import the necessary libraries for parsing JSON. In Java, there are several libraries available for JSON parsing such as:

  • org.json (JSON-java)
  • Gson
  • Jackson
  • JSON.simple

                import org.json.JSONObject; // For org.json (JSON-java)
                // Import other libraries as per your choice
            

Step 2: Creating JSON String

In order to parse JSON, we need to have a JSON string. You can either retrieve the JSON string from a web service or create it manually. In this example, we have the following JSON text:


                String jsonString = '{ "pageInfo": { "pageName": "abc", "pagePic": "http://example.com/content.jpg" }, "posts": [ { "post_id": "123456789012_123456789012", "actor_id": "1234567890", "picOfPersonWhoPosted": "http://example.com/photo.jpg", "nameOfPersonWhoPosted": "Jane Doe", "message": "Sounds cool. Can't wait to see it!", "likesCount": "2", "comments": [], "timeOfPost": "1234567890" } ] }';
            

Step 3: Parsing JSON

Now, we can parse the JSON string using the libraries mentioned earlier. Let's take a look at how we can parse JSON using each library:

Using org.json (JSON-java)


                JSONObject jsonObject = new JSONObject(jsonString);
                String pageName = jsonObject.getJSONObject("pageInfo").getString("pageName");
                String pagePic = jsonObject.getJSONObject("pageInfo").getString("pagePic");
                String postId = jsonObject.getJSONArray("posts").getJSONObject(0).getString("post_id");
            

In the above code snippet, we create a JSONObject from the JSON string and then retrieve the values of pageName, pagePic, and postId using the appropriate methods.

Using Gson


                import com.google.gson.Gson;
                import com.google.gson.JsonObject;
                
                Gson gson = new Gson();
                JsonObject jsonObject = gson.fromJson(jsonString, JsonObject.class);
                String pageName = jsonObject.getAsJsonObject("pageInfo").get("pageName").getAsString();
                String pagePic = jsonObject.getAsJsonObject("pageInfo").get("pagePic").getAsString();
                String postId = jsonObject.getAsJsonArray("posts").get(0).getAsJsonObject().get("post_id").getAsString();
            

In the above code snippet, we use Gson to parse the JSON string. We create a Gson object and then use the fromJson() method to convert the JSON string into a JsonObject. Finally, we retrieve the values of pageName, pagePic, and postId using the appropriate methods.

Using Jackson


                import com.fasterxml.jackson.databind.ObjectMapper;
                import com.fasterxml.jackson.databind.JsonNode;
                
                ObjectMapper objectMapper = new ObjectMapper();
                JsonNode jsonNode = objectMapper.readTree(jsonString);
                String pageName = jsonNode.get("pageInfo").get("pageName").asText();
                String pagePic = jsonNode.get("pageInfo").get("pagePic").asText();
                String postId = jsonNode.get("posts").get(0).get("post_id").asText();
            

In the above code snippet, we use Jackson to parse the JSON string. We create an ObjectMapper object and then use the readTree() method to convert the JSON string into a JsonNode. Finally, we retrieve the values of pageName, pagePic, and postId using the appropriate methods.

Using JSON.simple


                import org.json.simple.JSONObject;
                import org.json.simple.JSONArray;
                import org.json.simple.parser.JSONParser;
                
                JSONParser jsonParser = new JSONParser();
                JSONObject jsonObject = (JSONObject) jsonParser.parse(jsonString);
                String pageName = (String) jsonObject.get("pageName");
                String pagePic = (String) jsonObject.get("pagePic");
                JSONArray posts = (JSONArray) jsonObject.get("posts");
                String postId = (String) ((JSONObject) posts.get(0)).get("post_id");
            

In the above code snippet, we use JSON.simple to parse the JSON string. We create a JSONParser object and then use the parse() method to convert the JSON string into a JSONObject. Finally, we retrieve the values of pageName, pagePic, and postId using the appropriate methods.

Conclusion

In this article, we learned how to parse JSON in Java using different libraries. We covered the step-by-step process of parsing JSON and provided examples using org.json (JSON-java), Gson, Jackson, and JSON.simple. Depending on your requirements and preferences, you can choose the library that best suits your needs.