VOOZH about

URL: https://dzone.com/articles/how-can-we-read-a-json-file-in-java

⇱ How to Read and Parse a JSON File in Java


Related

  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 Updated Mar. 06, 25 · Tutorial
Likes
Comment
Save
101.2K 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

  • Apache Spark 3 to Apache Spark 4 Migration: What Breaks, What Improves, What's Mandatory
  • Introduction to Polymorphism With Database Engines in NoSQL Using Jakarta NoSQL
  • JSON Handling With GSON in Java With OOP Essence
  • Proper Java Exception Handling

Partner Resources

×

Comments

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

Let's be friends: