Back to: Java Tutorials For Beginners and Professionals
How to work with JSON in Java
In this article, I am going to discuss How to work with JSON in Java with Examples. Please read our previous article where we discussed the Java System.exit() Method.
What is JSON?
JavaScript Object Notation or briefly JSON is a data-interchange format that was introduced in 1999 and has become widely adopted within the mid-2000s. JSON or JavaScript Object Notation may be a lightweight text-based open standard designed for human-readable data interchange. Conventions employed by JSON are known to programmers, which include C, C++, Java, Python, Perl, etc. JSON is a generic data format that has six data types:
- Strings
- Numbers
- Booleans
- Arrays
- Objects
- Null
Sample JSON Document
JSON-simple library in Java
The JSON-simple is a lightweight library. It is used to process JSON objects. By using this you can read or, write the contents of a JSON document using Java program.
Uses of JSON
Since the JSON format is text only, JSON format can easily be sent to and from a server and used as a data format by any programming language because JSON format is text only. JavaScript has a built-in function to convert a string into native JavaScript objects, written in JSON format:
JSON.parse()
So, if you receive data from a server, in JSON format, you can use it like any other JavaScript object.
How to create a JSON document using a Java program?
To create a JSON document, we need to follow the following steps:
- Download json.simple jar file.
- Create a java project named “JSONSimpleExample” and paste downloaded jar JSON-simple-1.1.1.jar.
- Instantiate the JSONObject class of the JSON-simple library.
- Insert the required key-value pairs using the put() method of the JSONObject class.
- Write the created JSON object into a file using the FileWriter class.
Sample Program to work with JSON in Java:
import java.io.FileWriter; import java.io.IOException; import org.json.simple.JSONObject; public class CreateJSON { public static void main (String args[]) { //Creating a JSONObject object JSONObject jsonObject = new JSONObject (); //Inserting key-value pairs into the json object jsonObject.put ("ID", "1"); jsonObject.put ("First_Name", "Shikhar"); System.out.println ("\n"); jsonObject.put ("Last_Name", "Dhawan"); jsonObject.put ("Date_Of_Birth", "1981-12-05"); jsonObject.put ("Place_Of_Birth", "Delhi"); jsonObject.put ("Country", "India"); try { FileWriter file = new FileWriter ("D:/output.json"); file.write (jsonObject.toJSONString ()); file.close (); } catch (IOException e) { e.printStackTrace (); } System.out.println ("JSON file created: " + jsonObject); } }
Output
Output.json
How to read the content of the JSON file?
To read the content of the JSON file, we need to use the following steps:
- Instantiate the JSONParser class of the JSON-simple library.
- Parse the contents of the obtained object using the parse() method.
- Retrieve the value associated with a key using the get() method.
Sample Program to read the content of a JSON file in Java:
import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; public class ReadJSON { public static void main (String args[]) { //Creating a JSONParser object JSONParser jsonParser = new JSONParser (); try { //Parsing the contents of the JSON file JSONObject jsonObject = (JSONObject) jsonParser.parse (new FileReader ("D:/output.json")); String id = (String) jsonObject.get ("ID"); String first_name = (String) jsonObject.get ("First_Name"); String last_name = (String) jsonObject.get ("Last_Name"); String date_of_birth = (String) jsonObject.get ("Date_Of_Birth"); String place_of_birth = (String) jsonObject.get ("Place_Of_Birth"); String country = (String) jsonObject.get ("Country"); System.out.println ("Contents of the JSON are: "); System.out.println ("ID :" + id); System.out.println ("First name: " + first_name); System.out.println ("Last name: " + last_name); System.out.println ("Date of birth: " + date_of_birth); System.out.println ("Place of birth: " + place_of_birth); System.out.println ("Country: " + country); System.out.println (" "); } catch (FileNotFoundException e) { e.printStackTrace (); } catch (IOException e) { e.printStackTrace (); } catch (ParseException e) { e.printStackTrace (); } } }
Output
In the next article, I am going to discuss How to work with XML in Java with Examples. Here, in this article, I try to explain How to work with JSON in Java with Examples. I hope you enjoy this article.