DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. How Can We Read a JSON File in Java?

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.

Mahesh Sharma user avatar by
Mahesh Sharma
·
Nov. 02, 22 · Tutorial
Like (1)
Save
Tweet
Share
12.36K Views

Join the DZone community and get the full member experience.

Join For Free

Read 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.

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: 

  1. JSON Objects
  2. 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.

JSON Java (programming language)

Published at DZone with permission of Mahesh Sharma. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Front-End Troubleshooting Using OpenTelemetry
  • The Power of Zero-Knowledge Proofs: Exploring the New ConsenSys zkEVM
  • Building the Next-Generation Data Lakehouse: 10X Performance
  • Steel Threads Are a Technique That Will Make You a Better Engineer

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: