JSON.simple example – Read and write JSON
JSON.simple, is a simple Java library for JSON processing, read and write JSON data and full compliance with JSON specification (RFC4627).
In this tutorial, we show you how to use JSON.simple to read and write JSON data from / to a file.
1. JSON.simple Dependency
JSON.simple is available at Maven central repository, just declares following dependency in your pom.xml file.
<dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1</version> </dependency>
2. Write JSON to file
In below example, it write JSON data via JSONObject and JSONArray, and save it into a file named “test.json“.
import java.io.FileWriter; import java.io.IOException; import org.json.simple.JSONArray; import org.json.simple.JSONObject; public class JsonSimpleExample { public static void main(String[] args) { JSONObject obj = new JSONObject(); obj.put("name", "mkyong.com"); obj.put("age", new Integer(100)); JSONArray list = new JSONArray(); list.add("msg 1"); list.add("msg 2"); list.add("msg 3"); obj.put("messages", list); try { FileWriter file = new FileWriter("c:\\test.json"); file.write(obj.toJSONString()); file.flush(); file.close(); } catch (IOException e) { e.printStackTrace(); } System.out.print(obj); } }
Output – See content of file named “test.json“.
{ "age":100, "name":"mkyong.com", "messages":["msg 1","msg 2","msg 3"] }
3. Read JSON from file
Use JSONParser to read above generated JSON file “test.json“, and display each of the values.
import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Iterator; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; public class JsonSimpleExample { public static void main(String[] args) { JSONParser parser = new JSONParser(); try { Object obj = parser.parse(new FileReader("c:\\test.json")); JSONObject jsonObject = (JSONObject) obj; String name = (String) jsonObject.get("name"); System.out.println(name); long age = (Long) jsonObject.get("age"); System.out.println(age); // loop array JSONArray msg = (JSONArray) jsonObject.get("messages"); Iterator<String> iterator = msg.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } } }
Output
mkyong.com 100 msg 1 msg 2 msg 3

Big Help :)
Thank you very much!. Helped me.
Please visit http://www.luvcelebs.com to check more…
Simplest and very helpful tutorial. Thanks alot for keep me in safe jone.
Thank you so much……really very much beneficial it is :)
Simple example but unable to use. JsonParser is an abstract class
It is not abstract just go to this link…
http://juliusdavies.ca/json-simple-1.1.1-javadocs/org/json/simple/parser/JSONParser.html
example is very good just put your jar file in your class path and if u are using eclipse then add folder path in your referenced libraries….
Very Simple and Nice example, but does not work for me. JsonParser is an abstract class and cannot be instantiated.
Please add the jar file to ur project….Will work den..
Thank you it was pretty easy to understand.
Very nice one.. Thanks a lot.. :)
Thank you!
This is the simplest and most helpful tutorial/example I have found on JSON and Java (quoted form devGuru) too !!
I’m trying to read simple json file into my java code but it gives me ClassCastException
JSONParser parser = new JSONParser();
JSONObject jobj = (JSONObject) parser.parse(new FileReader(“src/test/resources/test.json”));
any idea what could be wrong with that?
hi, thanks for it, BUT i have question for it. if i only want to insert some data in this json file, NOT cover it, how i sould do?? thank you !
The following link is also useful if you want to use com.google.gson library to read JSON files : http://usmanali112.blogspot.com/2012/07/java-read-json-files.html
Hi, I’ve copied the code but it doesn’t work… the error is:Lexical Error: Unmatched Input.
Why??? what I should change?
I use the json file of the above example.
Thanks
good
Thanks, this example is simple and easy understandable.
Just wanted to thank you for providing a SIMPLE example of reading and writing JSON in java… for some reason every example out there wanted to over complicate the task for no reason.
What if I have JSON with more than one item? When I try to go through such file JSONSimple gives unexpected character error on left brace of the next object. Is it able to handle just one entry per file? This seems rather strange to me, I must be doing something wrong.
I have the same problem, maybe catch newlines would help
Thank you! This is the simplest and most helpful tutorial/example I have found on JSON and Java.