
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to read the 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, which 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 light weight library which is used to process JSON objects. Using this you can read or, write the contents of a JSON document using 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> 301 to 305 </dependencies>
Paste this with in the <dependencies> </dependencies> tag at the end of your pom.xml file. (before </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 −
- Instantiate the JSONParser class of the json-simple library.
JSONParser jsonParser = new JSONParser();
- Parse the contents of the obtained object using the parse() method.
//Parsing the contents of the JSON file JSONObject jsonObject = (JSONObject) jsonParser.parse(new FileReader("E:/players_data.json"));
- Retrieve the value associated with a key using the get() method.
String value = (String) jsonObject.get("key_name");
Following Java program parses the above created sample.json file, reads its contents and, displays them.
Example
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
- Related Articles
- How to read contents of a file using Scanner class?
- Golang Program to Read the Contents of a File
- How can we read a JSON file in Java?
- How to store the contents of arrays in a file using Java?
- How to read JSON file in Python
- How to read/parse JSON array using Java?
- How to read a JSON file into a DataFrame using Python Pandas library?
- How to Write/create a JSON file using Java?
- How to read the contents of a web page without using any external library in Java?
- How to read the contents of a webpage into a string in java?
- How to read an external JSON file in JavaScript
- C# Program to read contents of a file into a string at once
- How to read integers from a file using BufferedReader in Java?
- How to read data in from a file to String using java?
- How to write a JSON string to file using the Gson library in Java?
