top of page

How to achieve Data driven using JSON file

Understanding JSON:

JSON is a lightweight and human-readable data interchange format. It is easy for both machines and humans to read and write. JSON data is structured as a collection of key-value pairs, arrays, and nested objects. It can be easily consumed by various programming languages and technologies, making it suitable for data-driven web applications, APIs, and data integration tasks.

Why JSON over Excel:

JSON supports complex, hierarchical data structures. It can represent not only tabular data but also nested objects and arrays, making it more versatile

Excel is primarily designed for tabular data. It comes with its own limitations.for example MS office needs to be installed on the system where the test needs to be executed. While it can handle some level of hierarchy with multiple sheets, JSON is better suited for representing structured, nested, and deeply related data.

Using JSON data for data-driven testing, there is no need to include scenario outlines and examples in the feature file.

Step 1: Create JSON Data

The first step in achieving data-driven results with JSON is to create or obtain the JSON data that represents your dataset.

Here is an example :Create a new file in src/test/resources as Yourtestdataname.json

Step 2: JSON Reader The Data from the Json file can be read creating a Json reader class insrc/main/java under Utilities folder. Json reader class is shown below.

Here "readJsonData" method is responsible for reading JSON data. It takes a single parameter, filePath, which is a string representing the path to the JSON file you want to read. The method is expected to return a JsonObject.

FileReader reader = new FileReader(filePath); this object is used to read the contents of the file provided in the file path.

JsonObject jsonObject = JsonParser.parseReader(reader).getAsJsonObject(); JsonParser parses the JSON data from the reader and stores it in a JsonObject named jsonObject. The getAsJsonObject() method ensures that the parsed data is returned as a JsonObject. Try catch method to catch if any exception is thrown.

Step 3: Access the Data Once you have parsed the JSON data, you can easily access and manipulate its contents. You can navigate the JSON structure using keys or indexes, allowing you to retrieve and modify specific data elements.

Here, I have created an instance of the JSONReader class, which allows us to use its readJsonData method to read JSON data from a file. Specified the file path using a string variable (filepath) retrieved from a configuration property.

Used getAsJsonArray method to extract JSON data from the file (it's organized as an array named "Valid Data.")

then access individual elements within the JSON array by index and convert them to JsonObject instances.

Finally, extract specific values from the JsonObject using their keys, converting them to strings as needed.

Through this blog, I aim to illustrate how JSON data can be harnessed for data-driven testing, eliminating the need for scenario outlines and examples. Your thoughts and feedback upon reading it would be greatly appreciated.

189 views0 comments


Mit 0 von 5 Sternen bewertet.
Noch keine Ratings

Rating hinzufügen
bottom of page