×

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 transporting from server to Webpage. It is used in the field of “Web Development”.

The creating an object in JavaScript is like creating a JSON format as the JSON is originated from the JavaScript.

  • The JSON is text file so it can transfer easily.
  • The JSON doesn’t depend upon the specific language.

Syntax:

The data should be in the format of name, value pairs and the various data are separated by commas. The curly brackets are used to store objects and square brackets are used to store arrays. 

Features of JSON:

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. To execute the JSON program a “json.simple” jar must be install and set class path. The data structures used in JSON are:

  1. JSON objects
  2. JSON Arrays

In Detail:

  • 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 String and Values is represented any Datatypes which are 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 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 the given JSON file.

Example:

{
“name” : “Kotte”,
“college” : “BVRIT”
}

The above code is the file which 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.


Related Topics

What’s New in Java 15

Sealed classes are the new concept that was introduced by Java 15. Sealed classes are a preview feature. Most of the features which are released in java 15 are in...

3 minutes read.

Java Thread Dump Analyzer

Thread: A thread is a PC program that is stacked into the PC's memory and is under execution. It tends to be executed by a processor or a bunch of processors....

15 minutes read.

Resultset in java

Resultset: A result set is an interface that is present in the package java.sql and the resultset is used to store the data that are returned from the database table after...

5 minutes read.

Access modifiers in Java

Access modifiers in Java with Example In Java, there are two types of access modifiers one is a non-access modifier, and other is access modifier. If we talk about access modifier, there...

3 minutes read.

What are Array strings in Java?

In normal programming, An array is a group and a collection of identical forms of data that are stored in a sequential memory region and may be accessed using their...

4 minutes read.

Difference between Abstract Class and Interface

There is a similarity between abstract class and interface is that we cannot create objects for both of them. But irrespective of this, there are some differences between them, let’s...

2 minutes read.

How to use Lambda Expression in Java?

The new and significant lambda expression feature of Java was added in Java SE 8. It provides a clear and concise mechanism for describing a single method interface using an...

4 minutes read.

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

3 minutes read.

Delete a Cycle from a Linked List in Java

Assume a method detectAndRemoveLoop(), which examines if a given Linked List includes a loop and, if so, eliminates this Loop and returns true. This returns false if the list does...

7 minutes read.

Java Program to Add Digits Until the Number Becomes a Single Digit Number

We will develop Java programme that increase a number's digit count in order to reduce it to one. The issue is also known as the digit root problem. Example Consider the case where...

3 minutes read.

How to declare string array in Java

Introduction Array is a data structure with a fixed size, which allows us to store elements of similar type. Data of primitive types like int, char, float, string, etc. can be...

2 minutes read.

Three-way operator in Java

The ternary operator in Java is the only conditional operator that takes three operands. It's a popular one-line substitute for the if-then-else expression among Java programmers. If-else clauses can be...

3 minutes read.

if-else Program in Java

if-else Program in Java The if-else program in Java controls which code snippet will execute. The if-else program is very basic and yet very important. Because it checks how well one...

19 minutes read.

Streams in Java

The conventional Java SE 8 version came with many new peculiarities, out of which the most striking of which are assuredly lambda expressions and the method references. Streams and Streams...

14 minutes read.

Timestamp Operation in Java

JDBC escape syntax is supported by Timestamp's formatting and parsing functions. Additionally, it adds support for fractional seconds values for SQL TIMESTAMP.java.util.Date is wrapped in a lightweight wrapper that enables...

3 minutes read.

Hybrid Inheritance in Java

The most crucial OOPs concept in Java is inheritance, which enables the transfer of a class's properties to another class. It describes the Is-A relationship generally. We can create a...

3 minutes read.

Java Set Interface

We use set when we don't want to allow duplicate entries. All Set implementations do not allow duplicates. HashSet: This class stores its elements in hash tables. It uses the hashCode()...

3 minutes read.

Properties Class in Java

Properties class is associated with Java since JDK 1.0, i.e. it is a legacy class. It is the subclass of Hashtable. It is used to maintain the lists of values in which...

5 minutes read.

Java Array Generic

Creating Generic Array in Java A collection of comparable sorts of data is kept in an array. In Java, making a generic array is challenging. The type information of an array's...

4 minutes read.

How to Convert Hexadecimal to Decimal in Java

How to Convert Hexadecimal to Decimal in Java There are two methods to convert Hexadecimal to Decimal: Using parseInt() method Using user-defined logic Using Integer.parseInt() method It is a static method of...

2 minutes read.