Checkout for Promo Codes

How to read Json data using JsonReader?

This page shows an example on how to read json using JsonReader. Reading data using JsonReader is somewhat similar to XML parsing using DOM API.

As a first step add maven dependent jar files to your classpath. Here is the pom.xml file for your reference:

<project xmlns="" 
    xsi:schemaLocation=" ">

Input Json File:
    "emp_id": 1017,
    "emp_name": "Nagesh Y",
    "emp_designation": "Manager",
    "department": "Java2Novice",
    "salary": 30000,
    "direct_reports": [
        "Nataraj G",
    "address": {
        "street":"MG Road",

Here is the java example to read json data using JsonReader object:

package com.javaapi.json.examples;


import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;

public class ReadJsonExample {

	public static void main(String a[]){
		File jsonInputFile = new File("/Users/java2novice/jsonInput.txt");
		InputStream is;
		try {
			is = new FileInputStream(jsonInputFile);
			// Create JsonReader from Json.
			JsonReader reader = Json.createReader(is);
			// Get the JsonObject structure from JsonReader.
			JsonObject empObj = reader.readObject();
			// read string data
	        System.out.println("Emp Name: " + empObj.getString("emp_name"));
	        // read integer data
	        System.out.println("Emp Id: " + empObj.getInt("emp_id"));
			// read inner json element
	        JsonObject addrObj = empObj.getJsonObject("address");
	        System.out.println("City: " + addrObj.getString("city"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block

Emp Name: Nagesh Y
Emp Id: 1017
City: Bangalore
<< Previous Program | Next Program >>

Java API for JSON processing examples

  1. How to read Json data using JsonReader?
  2. How to read Json array data using JsonArray?
  3. How to create Json Object using Object Model?
  4. How to create Json Array using Object Model?
  5. How to create Json Object using Streaming Model API?
  6. How to create Json Array using Streaming Model API?
Knowledge Centre
Exception Vs Error
An error is an irrecoverable condition occurring at runtime like out of memory error. These kind of jvm errors cannot be handled at runtime. Exceptions are because of condition failures, which can be handled easily at runtime.
Famous Quotations
Tomorrow is often the busiest day of the week.
-- Spanish Proverb

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2020 by Nataraja Gootooru. All Rights Reserved.