How to Read JSON String in Java using json-simple library? Example Tutorial (original) (raw)

Hello guys, if you are wondering how to read JSON in Java using the json-simple library and looking for an example then you have come to the right place. Earlier, I have shown you how to parse JSON using Jackson and how to read JSON using the Gson library in Java, and today, I am going to share how to read JSON strings using json-simple, another popular JSON library in Java. If you don't know, JSON is a text format that is widely used as a data-interchange language because its parsing and its generation are easy for programs. It is slowly replacing XML as the most powerful data interchange format, as it is lightweight, consumes less bandwidth, and is also platform-independent.

Though Java doesn't have built-in support for parsing JSON files and objects, there are a lot of good open-source JSON libraries are available which can help you to read and write JSON objects to files and URLs. Two of the most popular JSON parsing libraries are Jackson and Gson. They are mature, rich, and stable.

Though there are a couple of more libraries there like JSON simple, which we are going to use in this example. Jackson and Gson later do a very nice job in mapping JSON objects and serialization. JSON is also used in requests and responses between client-server communication.

In this tutorial, we are going to see how to read and write JSON to file using JSON.Simple library, and you will notice how simple working with JSON is.

Since we don't have JSON support in JDK, we need to download this open-source library. If you are using maven to download the JAR and manage dependency, if not then you should, then you can just include the following dependencies in your pom.xml file :

<groupid>com.googlecode.json-simple</groupid>
**<**artifactid> json-simple</artifactid>
<version>1.1</version>

Otherwise, you have to add the newest version of json-simple-1.1.1.jar in CLASSPATH of your Java program. Also, Java 9 is coming up with built-in JSON support in JDK, which will make it easier to deal with JSON format, but that will not replace the existing Jackson and GSON library, which seems to be very rich with functionality.

How to create JSON File in Java? Example Tutorial

Here is a step-by-step guide on how to create a JSON file in Java and how to read and write on that. In this tutorial we are going to use JSON Simple open-source library, JSON.simple is a simple Java toolkit for JSON for encoding and decoding JSON text.

It is fully compliant with JSON specifications (RFC4627). It provides multiple functionalities such as reading, writing, parsing, escape JSON text while keeping the library lightweight. It is also flexible, simple, and easy to use by reusing Map and List interfaces. JSON.Simple also supports the streaming output of JSON text.

In this example, we have two methods for reading and writing JSON. JSONParser parses a JSON file and returns a JSON object.

Once you get JSONObject, you can get individual fields by calling the get() method and passing the name of the attribute, but you need to remember it to typecast in String or JSONArray depending upon what you are receiving.

Once you receive the array, you can use the Iterator to traverse through the JSON array. This way you can retrieve each element of JSONArray in Java. Now, let's see how we can write JSON String to a file.

How to read and write JSON String in Java using json-simple

Again we first need to create a JSONObject instance, then we can put data by entering the key and value. If you have not noticed the similarity then let me tell you, JSONObject is just like Map while JSONArray is like List.

You can see code in your write method, that we are using the put() method to insert value in JSONObject and using add() method to put the value inside the JSONArray object. Also note, the array is used to create a nested structure in JSON.

Once your JSON String is ready, you can write that JSON String to a file by calling toJSONString() method in JSONObject and using a FileWriter to write that String to the file.

import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Iterator;

import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser;

/**

}

Output: Writting JSON into file ... {"author":"J. K. Rolling","title":"Harry Potter and Half Blood Prince", "price":20,"characters":["Harry","Ron","Hermione"]} Done Reading JSON file from Java program title: Harry Potter and Half Blood Prince author: J. K. Rolling price: 20 characters: Harry Ron Hermione

That's all about how to parse JSON String in a Java program. You can also use other popular libraries like Gson or Jackson to do the same task. I like the JSON simple library to start with because it's really simple, and it provides a direct mapping between Java classes and JSON variables.

For example, String in Java also maps to string in JSON, java.lang.Number maps to number in JSON, and boolean maps to true and false in JSON, and as I have already said object is Map and array is List in Java.

All I can say is JSON is already a big thing and in the coming days every Java programmer is going to write more and more code to parse or encode decode JSON String, it's better to start early and learn how to deal with JSON in Java.

Other JSON tutorials you may like to explore

P.S. - If you want to learn how to develop RESTful Web Services using Spring Framework, check out Eugen Paraschiv's REST with Spring course. He has recently launched the certification version of the course, which is full of exercises and examples to further cement the real-world concepts you will learn from the course.