
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Read Contents of a JSON File Using Java
JSON or JavaScript Object Notation is a lightweight text-based open standard designed for human-readable data interchange. Conventions used by JSON are known to programmers and include C, C++, Java, Python, Perl, etc. Sample JSON document ?
{ "book": [ { "id": "01", "language": "Java", "edition": "third", "author": "Herbert Schildt" }, { "id": "07", "language": "C++", "edition": "second", "author": "E.Balagurusamy" } ] }
Json-simple library
The json-simple is a lightweight library which is used to process JSON objects. Using this, you can read or write the contents of a JSON document using a Java program.
JSON-Simple Maven Dependency
Following is the maven dependency for the JSON-simple library ?
<dependencies> <dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1.1</version> </dependency> </dependencies>
Paste this within the <dependencies> </dependencies> tag at the end of your pom.xml file (before the </project> tag).
Example
First of all, let us create a JSON document with name sample.json with the 6 key-value pairs as shown below ?
{ "ID": "1", "First_Name": "Shikhar", "Last_Name": "Dhawan", "Date_Of_Birth": "1981-12-05", "Place_Of_Birth":"Delhi", "Country": "India" }
To read the contents of a JSON file using a Java program Follow the steps given below −
Step 1: Instantiate the JSONParser class of the json-simple library.
JSONParser jsonParser = new JSONParser();
Step 2: Parse the contents of the obtained object using the parse() method.
//Parsing the contents of the JSON file FileReader fReader = new FileReader("E:/players_data.json") JSONObject jsonObject = (JSONObject) jsonParser.parse(fReader);
Step 3: Retrieve the value associated with a key using the get() method.
String value = (String) jsonObject.get("key_name");
The following Java program parses the above-created sample.json file, reads its contents, and displays them.
import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; public class ReadingJSON { public static void main(String args[]) { //Creating a JSONParser object JSONParser jsonParser = new JSONParser(); try { //Parsing the contents of the JSON file JSONObject jsonObject = (JSONObject) jsonParser.parse(new FileReader("E:/sample.json")); String id = (String) jsonObject.get("ID"); String first_name = (String) jsonObject.get("First_Name"); String last_name = (String) jsonObject.get("Last_Name"); String date_of_birth = (String) jsonObject.get("Date_Of_Birth"); String place_of_birth = (String) jsonObject.get("Place_Of_Birth"); String country = (String) jsonObject.get("Country"); //Forming URL System.out.println("Contents of the JSON are: "); System.out.println("ID :" + id); System.out.println("First name: " + first_name); System.out.println("Last name: " + last_name); System.out.println("Date of birth: " + date_of_birth); System.out.println("Place of birth: " + place_of_birth); System.out.println("Country: " + country); System.out.println(" "); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } } }
Output
Contents of the JSON are: ID :1 First name: Shikhar Last name: Dhawan Date of birth :1981-12-05 Place of birth: Delhi Country: India
Using Jackson Library
The Jackson library is also a powerful library for reading content JSON in Java. Below is a program to demonstrate how we can use this library:
import com.fasterxml.jackson.databind.ObjectMapper; import java.io.File; import java.io.IOException; public class ReadingJSONWithJackson { public static void main(String[] args) { ObjectMapper objectMapper = new ObjectMapper(); try { Person person = objectMapper.readValue(new File("E:/sample.json"), Person.class); System.out.println(person); } catch (IOException e) { e.printStackTrace(); } } } class Person { public String ID; public String First_Name; public String Last_Name; public String Date_Of_Birth; public String Place_Of_Birth; public String Country; @Override public String toString() { return "ID: " + ID + ", First Name: " + First_Name + ", Last Name: " + Last_Name + ", Date Of Birth: " + Date_Of_Birth + ", Place Of Birth: " + Place_Of_Birth + ", Country: " + Country; } }
Using Gson Library
The Gson library is developed by Google and it also helps us to read JSON content in java. The below program demonstrates the process to achieve it −
import com.google.gson.Gson; import java.io.FileReader; import java.io.IOException; public class ReadingJSONWithGson { public static void main(String[] args) { Gson gson = new Gson(); try (FileReader reader = new FileReader("E:/sample.json")) { Person person = gson.fromJson(reader, Person.class); System.out.println(person); } catch (IOException e) { e.printStackTrace(); } } }