Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Read Simple JSON in Java

Using JsonReader method in JSonObject, we can read a simple Json and display Json content on the console. This article explains how to read a simple Json content. Create a maven based project and add the following dependencies in the pom.xml file. 

 

<dependency>
	<groupId>org.codehaus.woodstox</groupId>
	<artifactId>woodstox-core-asl</artifactId>
	<version>4.1.4</version>
</dependency>
<dependency>
	<groupId>javax.xml.stream</groupId>
	<artifactId>stax-api</artifactId>
	<version>1.0-2</version>
	<scope>compile</scope>
</dependency>
<dependency>
	<groupId>org.glassfish</groupId>
	<artifactId>javax.json</artifactId>
	<version>1.0.4</version>
</dependency>

The following Json is in the file named "test.json". We read the following JSON and output them on the console. 

{
	"TestName": "Login",
	"TestModule": "Dashboard",
	"TestType": "Regression",
	"TestSteps": [{
		"Id": 1,
		"Step": "Open browser"
	},
	{
		"Id": 2,
		"Step": "Enter user name"
	},
	{
		"Id": 3,
		"Test": "Enter user password"
	},
	{
		"Id": 4,
		"Step": "Click on Login button"
	}],
	"Verification": "Logout link should display",
	"Note": ""
}

write the following code in the class "ReadSimpleJason.java"

package com.example.Json;
import javax.json.*;
import java.io.*;
public class ReadSimpleJason {
    public static void main(String[] args)
    {
        try {
            ReadJason();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void ReadJason() throws IOException {

        //define Input Stream for the Json file
        InputStream inputStream=null;
        //define Json Reader
        JsonReader jsonReader=null;
        //define Json Object
        JsonObject jsonObject=null;
        try {
            inputStream = new FileInputStream("test.json");
            jsonReader=Json.createReader(inputStream);
            jsonObject = jsonReader.readObject();
           System.out.println(jsonObject.toString());
           System.out.println(jsonObject.get("TestName"));
         JsonArray testSteps=jsonObject.getJsonArray("TestSteps");
            int numberOfSteps=testSteps.size();
            for(int i=0;i<numberOfSteps;i++)
            {
                JsonObject eachStep=testSteps.getJsonObject(i);
                System.out.println("Id  "+eachStep.get("Id") + " Step:  "+eachStep.get("Step"));
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
finally {
            inputStream.close();
            jsonReader.close();
        }
    }

}

Run the code and see the console result below.

{"TestName":"Login","TestModule":"Dashboard","TestType":"Regression","TestSteps":[{"Id":1,"Step":"Open browser"},{"Id":2,"Step":"Enter user name"},{"Id":3,"Step":"Enter user password"},{"Id":4,"Step":"Click on Login button"}],"Verification":"Logout link should display","Note":""}
"Login"
Id  1 Step:  "Open browser"
Id  2 Step:  "Enter user name"
Id  3 Step:  "Enter user password"
Id  4 Step:  "Click on Login button"