Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Read Json Object Using Gson in Java

Google Gson is a Java library that can be used to convert Java Objects into respective JSON format. This article explains how to use Google Gson to convert Java Object to JSon and read its content. We are reading the following Json content from a file "testGson.json".

 

{
  "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": ""
}

Create a maven based project and add Google Gson dependency in the pom.xml file

<dependency>
 <groupId>com.google.code.gson</groupId>
 <artifactId>gson</artifactId>
 <version>2.3.1</version>
</dependency>

Create Java Object TestCases and TestSteps

TestSteps.java

package com.example.Json;

public class TestSteps {
    //define id field
    private int Id;
    //define step field
    private String Step;
    //create a construct
    public TestSteps() {
    }

    public TestSteps(int id, String step) {
        Id = id;
        Step = step;
    }

    public int getId() {
        return Id;
    }

    public void setId(int id) {
        Id = id;
    }

    public String getStep() {
        return Step;
    }

    public void setStep(String step) {
        Step = step;
    }
}

TestCases.java

package com.example.Json;
import java.util.List;
public class TestCases {
    //define TestName filed
    private String TestName;
    //define TestModule field
    private String TestModule;
    //define TestType field
    private String TestType;
    //define TestSteps field
    private List TestSteps;
    //define Verification field
    private String Verification;
    //define Note field
    private String Note;
    //create an empty construct
    public TestCases() {
    }
    //create a construct with all the fields
    public TestCases(String testName, String testModule, String testType, List<com.example.Json.TestSteps> testSteps, String verification, String note) {
        TestName = testName;
        TestModule = testModule;
        TestType = testType;
        TestSteps = testSteps;
        Verification = verification;
        Note = note;
    }
    public String getTestName() {
        return TestName;
    }

    public void setTestName(String testName) {
        TestName = testName;
    }

    public String getTestModule() {
        return TestModule;
    }

    public void setTestModule(String testModule) {
        TestModule = testModule;
    }

    public String getTestType() {
        return TestType;
    }

    public void setTestType(String testType) {
        TestType = testType;
    }

    public List<com.example.Json.TestSteps> getTestSteps() {
        return TestSteps;
    }

    public void setTestSteps(List<com.example.Json.TestSteps> testSteps) {
        TestSteps = testSteps;
    }

    public String getVerification() {
        return Verification;
    }

    public void setVerification(String verification) {
        Verification = verification;
    }

    public String getNote() {
        return Note;
    }

    public void setNote(String note) {
        Note = note;
    }
}

Write the following code in the ReadJsonWithGson.java class

package com.example.Json;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

public class ReadJsonWithGson {
    public static void main(String[] args)
    {
        Gson gson=new Gson();
        try {
            System.out.println("Reading JSON from a file");
            System.out.println("----------------------------");

            BufferedReader br = new BufferedReader(new FileReader("testGson.json"));
            //convert the json string back to object
            TestCases testCasesObj = gson.fromJson(br, TestCases.class);

            System.out.println("Name Of Test Name: "+testCasesObj.getTestName());
            System.out.println("Name of Test Module: "+testCasesObj.getTestModule());
            System.out.println("Name of Test Verification: "+testCasesObj.getVerification());
            System.out.println("Test Steps are :");

            List listOfTestSteps = testCasesObj.getTestSteps();
            for (int i = 0; i < listOfTestSteps.size(); i++) {
                System.out.println("Id: " +listOfTestSteps.get(i).getId() + "   Step:  " + listOfTestSteps.get(i).getStep());
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Run the code and see the result on the console. The file "testGson.json"  content is displayed. 

{
  "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": ""
}