top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Handling JSON file with GSON Library

JSON stands for Javascript Object Notation.JSON .It is a lightweight data interchange format.It is easy for humans to read and write and is easy for machine to parse and generate.Json syntax is derived from javascript object notation,but format is text only.




JSON is a way of communicating data with specific rules

  •  Set of name/value pairs

  •  Each name is followed by colon:

  •  An object begins with curly brace{

  and ends with closed curly brace}

  • Square brackets holds array



Here's a step by step process of how to use a JSON file for data parametrization in Selenium


Step 1:Adding JSON plugin in Help>>EclipseMarketPlace






Step 2: Add following dependency in pom.xml 

In this we are reading data from JSON file through GSON approach.so we are using GSON dependency.


<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->

<dependency>

<groupId>com.google.code.gson</groupId>

<artifactId>gson</artifactId>

<version>2.11.0</version>

</dependency>



Step 3: Creating JSON file

Our JSON file looks like this when it is created. Based on your data set you can create json file


There are 2 types of formats simple json and JSON array.

here data is stored in simple format which can be accessed easily . The JSON variable define it as an object which contains many values.


{

 persondetails={

    "firstName": "Json",

    "lastName" : "Gson",

     "street": "abc",

     "city": "Chittoor",

    "state": "AP"

   };



JSON Array:

It supports storing multiple sets of data in the form of array. This can be acheived by keeping multiple objects in square brackets with in one json structure as below.


Below is our sample data used in this example which is having 2 fields firstname and lastname and with address another field which is a json array ("mentioned in [") having 2 json objects and again containing with 3 fields(Street,City,State).


{

 

    "firstName": "Json",

    "lastName" : "Gson",

   "address": [

   {

   "street": "abc",

   "city": "Chittoor",

   "state": "AP"

   },

   {

   "street": "efg",

   "city": "Chennai",

   "state": "TN"

   }

]

}


STEP 4: Create a class ,matches to the JSON file details


We need to create a java class which should exactly match with json file details.that is, how many attributes are there in java class need to create those many variables in java class for every variable need to create get and set methods so we can operate variables .


Here in this example created 2 classes person details and address


Below , person details is a main class defines with first name and last name ,with get set methods.

In address field having multiple values and representing array so created a seperate class called address which is mentioned next steps.


So created address variable in person details class List<Address> address;


Person details is a class exactly representing data which is in Testdata1.json file


package jsonfile;

import java.util.List;

import org.apache.poi.ss.formula.functions.Address;

public class Persondetails {

String firstName;

String lastName;

List<Address> address;

public String getFirstName() {

return firstName;

}

public void setFirstName(String firstName) {

this.firstName=firstName;

}

public String getLastName() {

return firstName;

}

public void setLastName(String lastName) {

this.lastName=lastName;

}

public List<Address> getAddress(){

return address;

}


}




Below is the example of address class as mentioned in above steps.


package jsonfile;


public class Address {


String street;

String city;

String state;


public String getStreet() {

return street;

}

public void setStreet(String street) {

this.street=street;

}

public String getCity() {

return city;

}

public void setCity(String city) {

this.city=city;

}

public String getState() {

return city;

}

public void setState(String state) {

this.state=state;

}

}



}



STEP5: Create a class to read data from JSON file


In this step, we already have a json file so need to get json file into a variable for that we need file reader and specify path of the json file .this line shows as below


FileReader reader=new FileReader(".\\data\\Testdata1.json");


Next we need to map with java object so need to create object of GSON which will come from GSON library as shown below


Gson gson=new Gson();


Next, using with this gson object need to call one method fromjson and here need to pass 2 parameters one is file reader and the other is to map with persondetails class this lines represents as shown below


Persondetails person=gson.fromJson(reader, Persondetails.class);


Next is address is a object array which has multiple values so ,writing with loop statement so for that taking person.getaddress it returns multiple addresses in the for of list. so, that is stored in address .From the person details in address stored in a variable so created address variable for which we have created class too.

as shown below


for(Address address : person.getAddress())


From this we can exctract we can extract Street,city,State.


Below is the final code and represents as, first we get a file and map that to the person class through the fromjson method that will return person class object and then with that object we can easily get data just by calling the methods from that object.



public class ReadJSON {


public static void main(String[] args) throws FileNotFoundException {

FileReader reader=new FileReader(".\\data\\Testdata1.json");

Gson gson=new Gson();

Persondetails person=gson.fromJson(reader, Persondetails.class);


//access data

System.out.println("Firstname:"+person.getFirstName());

System.out.println("Last name:"+person.getLastName());


for(Address address : person.getAddress())

{

System.out.println("Street:"+address.getStreet());

System.out.println("City:"+address.getCity());

System.out.println("State:"+address.getState());

}

}



}



Conclusion:

We can implement same with data driven testing using JSON with cucumber. There are 3 approaches in handling JSON file Jackson library,GSON library and JSON Simple library. Anyone approach we can follow. Here we used GSON library.Hope this was helpful.

Thanks for reading . see you until next blog






42 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page