How can we read JSON file in Java?

read JSON file in Java

to learn about Reading a JSON file in Java, First of all, we need to know about the JSON file.

JSON

JSON is “JavaScript Object Notation.” JSON is a simple format for storing and sending data. It is used when moving data from server to webpage. It is used in the field of “web development”.

Creating objects in JavaScript is like creating a JSON format, as JSON is derived from JavaScript.

  • JSON is a text file from which it can be easily transferred.
  • JSON does not depend on the specific language.

syntax

The data must be in the format of name and value pairs, and commas separate the different data. Curly brackets are used to store objects, and square brackets are used to store arrays.

Features of JSON

Following are some of the features of JSON:

  • easy
  • platform independent
  • easy to move
  • extensibility
  • interoperability

data types

Following are some of the datatypes:

  • string A string is represented inside quotation marks.
  • number– It represents numeric characters.
  • boolean – It contains true or false.
  • Zero – empty

JSON in Java

To use JSON in Java, we must use “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:

  1. JSON Objects
  2. JSON arrays

JSON Objects

JSON objects are represented between curly brackets. The object must be in a key/value pair. The key is represented as a string, and the values ​​represent any of the datatypes described above.

Example:

Key, value pairs – {“Name”: “Kotte”} 

JSON arrays

JSON Arrays are used to store objects. These items 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 students data is stored as an object.

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 a JSON file in Java, the “FileReader()” method is used to read the given JSON file.

Example:

{

            “name” : “Kotte”,

            “college” : “BVRIT”

}

The above code is the file that is used to read. We use “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, JSONParser().parse() is used, which is present in “org.json.simple.parser.*” to parse the File.json file.

Leave a Comment