JSON Files

Introduction

In Data-Driven Testing, automation scripts often require structured test data such as user information, product details, API payloads, or application configurations. While Excel and CSV files are useful for storing simple tabular data, they are not suitable for representing complex or hierarchical data.

This is where JSON (JavaScript Object Notation) becomes extremely useful. JSON is a lightweight data-interchange format that stores information using key-value pairs, arrays, and nested objects. It is easy for both humans and machines to read and write.

Python provides a built-in json module that allows automation scripts to read and write JSON files without installing any additional libraries.

In Selenium automation, JSON files are commonly used to store test data, application configuration, API request bodies, expected responses, user credentials, and environment-specific information.

In this tutorial, you’ll learn what JSON files are, why they are used in Selenium automation, how to read JSON data using Python, and how to use that data for Data-Driven Testing.


What is a JSON File?

A JSON (JavaScript Object Notation) file is a lightweight text file used to store structured data in the form of key-value pairs.

Unlike CSV files, JSON can represent nested objects and arrays, making it ideal for storing complex data.

Example JSON file:

{
    "message": "JSON Message"
}

The data can then be read by Python and used inside Selenium automation scripts.


Why Use JSON Files?

JSON files are widely used in automation frameworks because they offer several advantages:

  • Store structured and hierarchical data.

  • Easy to read and write.

  • Built into Python through the json module.

  • Ideal for configuration files.

  • Commonly used in API Testing.

  • Supports nested objects and arrays.

  • Separates test data from test scripts.

  • Improves framework maintainability.


Example

import json

from selenium import webdriver
from selenium.webdriver.common.by import By


def test_json_files(tmp_path):
    data_path = tmp_path / "message.json"

    data_path.write_text(
        json.dumps({"message": "JSON Message"}),
        encoding="utf-8"
    )

    data = json.loads(
        data_path.read_text(encoding="utf-8")
    )

    driver = webdriver.Chrome()

    try:
        driver.get("https://www.testmuai.com/selenium-playground/simple-form-demo")
        driver.find_element(By.ID, "user-message").send_keys(data["message"])
        driver.find_element(By.ID, "showInput").click()

        assert driver.find_element(By.ID, "message").text == data["message"]
    finally:
        driver.quit()

Understanding the Code

Import Required Modules

import json

from selenium import webdriver
from selenium.webdriver.common.by import By

The required modules are imported.

  • json is Python’s built-in module used to read and write JSON files.

  • webdriver launches the browser.

  • By is used to locate web elements.


Create the JSON File

data_path = tmp_path / "message.json"

A temporary JSON file named message.json is created.

Note: In real-world automation frameworks, the JSON file already exists and is usually stored inside a folder such as testdata, resources, or config. The automation script simply reads the existing file instead of creating it every time.


Write Data into the JSON File

data_path.write_text(
    json.dumps(
        {
            "message": "JSON Message"
        }
    ),
    encoding="utf-8"
)

json.dumps() converts a Python dictionary into JSON format.

The generated JSON file contains:

{
    "message": "JSON Message"
}

Here,

  • "message" is the key.

  • "JSON Message" is the value.


Read the JSON File

data = json.loads(
    data_path.read_text(
        encoding="utf-8"
    )
)

The JSON file is read and converted back into a Python dictionary.

The variable data now contains:

{
    "message": "JSON Message"
}

Retrieve the Required Value

data["message"]

This retrieves the value associated with the key "message".

The retrieved value is:

JSON Message

Launch the Browser

driver = webdriver.Chrome()

A new Chrome browser instance is created.


Open the Application

driver.get(
    "https://www.testmuai.com/selenium-playground/simple-form-demo"
)

The browser navigates to the Selenium Playground Simple Form Demo page.


Enter the JSON Data

driver.find_element(
    By.ID,
    "user-message"
).send_keys(
    data["message"]
)

The value read from the JSON file is entered into the message textbox.

Instead of hard-coding the input value, Selenium uses the data retrieved from the JSON file.


Click the Button

driver.find_element(
    By.ID,
    "showInput"
).click()

The Show Message button is clicked.

The application displays the entered message.


Verify the Result

assert driver.find_element(
    By.ID,
    "message"
).text == data["message"]

The displayed message is compared with the value read from the JSON file.

If both values match, the test passes successfully.


Close the Browser

finally:
    driver.quit()

The browser is closed after the test execution.


Practical Example

Suppose an e-commerce website allows users to update their profile information.

Instead of storing customer details directly inside the Selenium script, all user information such as name, email address, phone number, and city is stored in a JSON file. During execution, Selenium reads the JSON data and fills the profile form automatically.


Automation Testing Example

Consider an online banking application where different customer accounts need to be validated.

Customer information such as account number, username, password, branch, and account type is stored in a JSON file. The automation framework reads this structured data and performs login and transaction validation for multiple customers without modifying the test script.


Real-World Example

JSON files are extensively used in automation frameworks for:

  • Banking Applications

  • E-commerce Websites

  • CRM Systems

  • ERP Applications

  • Healthcare Portals

  • Insurance Systems

  • HR Management Systems

  • Enterprise Web Applications

  • REST API Testing

Typical JSON data includes user credentials, application settings, product information, API request bodies, API responses, customer details, transaction information, and environment configurations.


Advantages of Using JSON Files

  • Stores structured and hierarchical data.

  • Supports nested objects and arrays.

  • Built-in Python support through the json module.

  • Easy to read and maintain.

  • Ideal for configuration management.

  • Widely used in API Testing.

  • Supports Data-Driven Testing.

  • Separates test data from automation logic.


Common Mistakes Beginners Make

Writing Invalid JSON

Ensure the JSON structure follows proper syntax with matching braces, quotation marks, and commas.


Confusing Python Dictionaries with JSON

Remember that JSON is a text format, while Python uses dictionaries internally after parsing the JSON file.


Using Incorrect Keys

Always verify that the key names used in the code exactly match those in the JSON file.


Hard-Coding Test Data

Avoid writing input values directly inside Selenium scripts.

Store reusable test data in JSON files whenever appropriate.


Best Practices

  • Store reusable test data in JSON files.

  • Use meaningful key names.

  • Keep JSON files well-formatted and properly indented.

  • Separate configuration data from test data.

  • Validate that required keys exist before accessing them.

  • Organize JSON files inside a dedicated testdata or config folder.

  • Use JSON for structured or nested data instead of CSV.


Conclusion

JSON Files provide an efficient way to store structured test data for Selenium automation. Using Python’s built-in json module, automation scripts can easily read and process JSON data without additional libraries. JSON files are widely used for Data-Driven Testing, configuration management, and API Testing, making them an essential part of modern Selenium automation frameworks.


Frequently Asked Questions (FAQs)

What is a JSON file?

A JSON (JavaScript Object Notation) file is a text file used to store structured data using key-value pairs, arrays, and nested objects.


Why are JSON files used in Selenium automation?

JSON files help store structured test data, configuration settings, and API-related information while keeping test scripts clean and reusable.


Which Python module is used to work with JSON files?

Python provides the built-in json module for reading and writing JSON files.


What is the difference between json.dumps() and json.loads()?

  • json.dumps() converts a Python object into a JSON-formatted string.

  • json.loads() converts a JSON-formatted string into a Python object.


Are JSON files commonly used in professional automation frameworks?

Yes.

JSON files are widely used in Selenium automation frameworks for Data-Driven Testing, configuration management, and REST API Testing.


Key Takeaways

  • JSON Files store structured data using key-value pairs.

  • Python’s built-in json module is used to read and write JSON files.

  • JSON supports nested objects and arrays, making it more flexible than CSV.

  • JSON files help separate test data from automation logic.

  • They are widely used in Data-Driven Testing, configuration management, and API Testing.