How Can We Read a JSON File in Java?
JSON is the simple format for storing and sending data. It is used when the data is transported from the server to the Webpage.
Join the DZone community and get the full member experience.
Join For FreeRead JSON File in Java
To know about reading a JSON file in Java, first, we must know about the JSON file.
JSON
JSON is “JavaScript Object Notation.” The JSON is the simple format for storing and sending data. It is used when the data is transported from the server to the Webpage. It is used in the field of “Web Development.”
Creating an object in JavaScript is like creating a JSON format, as the JSON originated from JavaScript.
- The JSON is a text file so that it can transfer easily.
- The JSON doesn’t depend upon the specific language.
The Ultimate Java Expert Certification Bundle.*
*Affiliate link. See Terms of Use.
Syntax
The data should be in the format of names and value pairs, and commas separate the various data. The curly brackets are used to store objects, and square brackets are used to store arrays.
Features of JSON
The following are some features of the JSON:
- Simple
- Platform Independent
- Easy to transfer
- Extensibility
- Interoperability
Datatypes
Following are some of the datatypes:
- String – A String is represented inside the quotation marks.
- Number- It represents Numeric characters.
- Boolean – It consists of either true or False.
- Null – Empty
JSON In Java
To use the JSON in java, we must use the “json.simple” library to encode and decode. A “json.simple” jar must be installed to execute the JSON program and set the class path. The data structures used in JSON are:
- JSON Objects
- JSON Arrays
JSON Objects
The JSON objects are represented in between the curly brackets. The objects should be in the Key/value pairs. The Key is represented as a String, and Values represent any Datatypes mentioned above.
Example:
Key, value pairs – {“Name”: “Kotte”}
JSON Arrays
The JSON Arrays are used to store the objects. These objects are enclosed in square brackets[].
Example:
[{
“Name” : ”Kotte”,
“College” : ”BVRIT”
“Branch” : “Computer Science Engineering”,
“Section” : “CSE_C”
},
{
“Name” : ”Saikiran”,
“College” : ”BVRIT”
“Branch” : “Computer Science Engineering”,
“Section” : “CSE_C”
}]
The above example shows the student details as an array, and inside the array, the data of students are stored as objects.
A Simple Program for JSON in Java
import org.json.simple.JSONObject;
public class Json
{
public static void main(String args[])
{
JSONObject j = new JSONObject();
j.put(“Name”, “Kotte”);
j.put(“College”, “BVRIT”);
j.put(“Branch” , “Computer science engineering”);
j.put(“Section”, “CSE-C”);
System.out.println(j);
}
}
Output:
{“Name”: “Kotte”, “College” : “BVRIT”, “Branch” : “Computer Science Engineering”, “Section” : “CSE-C”}
Reading JSON File in Java
To read the JSON file in Java, “FileReader()” method is used to read given JSON file.
Example:
{
“name” : “Kotte”,
“college” : “BVRIT”
}
The above code is the file that is used to read. we use the “json.simple” library.
//program for reading a JSON file
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.*;
public class JSON
{
public static void main(Strings args[])
{
// file name is File.json
Object o = new JSONParser().parse(new FileReader(File.json));
JSONObject j = (JSONObject) o;
String Name = (String) j.get(“Name”);
String College = (String ) j.get(“College”);
System.out.println(“Name :” + Name);
System.out.println(“College :” +College);
}
}
Output:
Name: Kotte
College: BVRIT
In the above program, the JSONParser().parse() is used, which is present in the “org.json.simple.parser.*” to parse the File.json file.
Published at DZone with permission of Mahesh Sharma. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments