×

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

Java Integer highestOneBit()

The highestOneBit() method of Java Integer class returns an int value with at most a single one-bit, in the position of the highest-order one-bit in the specified int value.  Syntax public static...

1 minute read.

Java Public Keyword

A Java access modifier is a public keyword. It can be applied to classes, constructors, methods, and variables. It is the type of access modifier that is least constrained. A...

4 minutes read.

How to Convert Decimal to Octal in Java

How to Convert Decimal to Octal in Java There are two methods to convert Decimal to Octal: Using toOcatlString() method Using user-defined logic Using Integer.toOctalString() method The toOctalString() is astaticmethod of the Integer...

2 minutes read.

How to generate random numbers in Java

Random numbers, also known as fake numbers, are actually a part of a very large sequence, so they are called random numbers. In a defined set of numbers, every number...

6 minutes read.

How to Create Singleton Class in Java

In this tutorial, we will discuss the singleton class and how to create it. Introduction Java is a purely object-oriented programming language. It consists of classes and objects. But Singleton is one...

4 minutes read.

Association in Java

In Java, association refers to the link between two classes established by their objects. One-to-one, one-to-many, and many-to-many connections are managed via association. The Association defines the multiplicity between objects...

5 minutes read.

Types of Bitwise Operators in Java

In this tutorial, we will learn about the various types or kinds of bitwise operators in java. Before proceeding to bitwise operators, let us know what is meant by the...

6 minutes read.

Hogben Numbers in Java

In this section, we will discover what the Hogben number is and develop Java programs that compute it. Java coding interviews and academic exams typically involve questions about the Hogben...

3 minutes read.

Types of Statements in Java

In natural languages, statements and sentences are roughly equivalent. In general, statements are similar to valid English sentences. We will talk about a statement in Java and the different kinds...

11 minutes read.

Block Swap Algorithm for array rotation in Java

An array and r, the rotation factor by which the array must be rotated, are both provided to us. We must then return the rotated array. A well-known and popular method is...

4 minutes read.

How to Change the Day in the Date using Java?

To operate with the date and time in Java, we need the Calendar abstract class. It provides a number of helpful interfaces that enable us to convert dates between a...

4 minutes read.

Number Pattern Programs in Java

Number Pattern Programs in Java: Number pattern programs are part of pattern programs. In the previous section, we have learned the approach to print the pattern program in Java. To...

6 minutes read.

How to Convert long to String in Java

How to Convert long to String in java It is used if we have to display a long number in the text field because everything is displayed as a String. A...

3 minutes read.

Deadlock Prevention and avoidance in Java

This tutorial will discuss deadlock prevention and Avoidance in Java programming language. Introduction Multithreading in Java includes deadlock. We can multitask by running several threads concurrently in the multithreading environment. Deadlock occurs...

4 minutes read.

Bubble Sort in Java

Bubble Sort in Java Bubble sort isalso known as sinking sort. It is one of the simplest sorting algorithms. In the bubble sort algorithm, the given array is traversed from left...

5 minutes read.

How to Create an Immutable Class in Java

How to Create an Immutable Class in Java In Java, immutable means something that cannot be change. A class is called an immutable class if its content cannot be changed, once...

3 minutes read.

Java String toLowerCase() methods

Java String toLowerCase() method is used to convert all the characters of the String into lower case. Syntax: public String toLowerCase()              public String toLowerCase(Locale locale) Returns: It returns Lower...

1 minute read.

Java logger

Logging is a crucial Java feature that aids developers in identifying issues. The logging methodology is included with Java as the programming language. It offers the Logging API, which debuted...

7 minutes read.

Java Integer rotateLeft() method

The rotateLeft() method of Java Integer class returns the value obtained by rotating the  2’s complement binary representation of the given integer value left by the specified number of bits. Syntax public...

1 minute read.