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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Introduction to Polymorphism With Database Engines in NoSQL Using Jakarta NoSQL
  • JSON Handling With GSON in Java With OOP Essence
  • Proper Java Exception Handling
  • Postgres JSON Functions With Hibernate 6

Trending

  • How to Format Articles for DZone
  • From Zero to Production: Best Practices for Scaling LLMs in the Enterprise
  • Unlocking AI Coding Assistants Part 1: Real-World Use Cases
  • Medallion Architecture: Why You Need It and How To Implement It With ClickHouse
  1. DZone
  2. Coding
  3. Java
  4. How to Read and Parse a JSON File in Java

How to Read and Parse 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.

By 
Mahesh Sharma user avatar
Mahesh Sharma
·
Updated Mar. 06, 25 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
87.6K 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 short for “JavaScript Object Notation.” 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.


Java Programming and Software Engineering Fundamentals*

*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 JSON:

  • Simple
  • Platform independent
  • Easy to transfer
  • Extensibility
  • Interoperability

Data Types

The following are some of the data types:

  • String – This 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 data types mentioned above. 

Example:

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


JSON Arrays

The JSON arrays are used to store the objects. These objects are enclosed in square brackets [].

Example:

JSON
 
[{

“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

Input:

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:

Java
 
{“Name”: “Kotte”, “College” : “BVRIT”, “Branch” : “Computer Science Engineering”, “Section” : “CSE-C”} 


Reading the JSON File in Java

To read the JSON file in Java, FileReader() method is used to read the given JSON file.

Example:

Java
 
{

            “name” : “Kotte”,

            “college” : “BVRIT”

}

The above code is the file that is used to read. we use the json.simple library. 

Input:

Java
 
//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:

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

To see more JSON libraries, you can visit this GitHub page.

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.

Related

  • Introduction to Polymorphism With Database Engines in NoSQL Using Jakarta NoSQL
  • JSON Handling With GSON in Java With OOP Essence
  • Proper Java Exception Handling
  • Postgres JSON Functions With Hibernate 6

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!