JSON Basics

Introduction

JSON (JavaScript Object Notation) is one of the most widely used data formats for storing and exchanging information. It is lightweight, easy to read, and supported by almost every modern programming language.

In Node.js, JSON is commonly used to store configuration files, exchange data between client and server, read API responses, save application settings, and manage structured data.

For automation engineers, JSON is an essential format because REST APIs typically send and receive data in JSON format. Automation frameworks such as Playwright, Selenium, Cypress, and API testing tools frequently use JSON for request payloads, responses, configuration files, and test data.

In this tutorial, you’ll learn the fundamentals of JSON and how it is used in Node.js applications.


What is JSON?

JSON (JavaScript Object Notation) is a lightweight text-based format used to represent structured data.

It stores information using key-value pairs, making it easy for both humans and machines to understand.

Example:

{
    "name": "John",
    "age": 25,
    "city": "New York"
}

Why Use JSON?

JSON is widely used because it is:

  • Lightweight

  • Human-readable

  • Easy to write

  • Easy to parse

  • Language-independent

  • Supported by almost every programming language

  • Ideal for data exchange between applications


JSON Structure

A JSON object consists of key-value pairs enclosed in curly braces.

Example:

{
    "firstName": "Alice",
    "lastName": "Johnson",
    "age": 30
}

In this example:

  • firstName, lastName, and age are keys.

  • "Alice", "Johnson", and 30 are values.


JSON Data Types

JSON supports the following data types:

Data TypeExample
String"John"
Number25
Booleantrue
Nullnull
Object{ "city": "London" }
Array["Java", "Python", "Node.js"]

JSON Object Example

{
    "id": 101,
    "name": "David",
    "department": "IT",
    "active": true
}

JSON Array Example

[
    "Apple",
    "Banana",
    "Orange"
]

Array of JSON Objects

[
    {
        "id": 1,
        "name": "John"
    },
    {
        "id": 2,
        "name": "Alice"
    },
    {
        "id": 3,
        "name": "Bob"
    }
]

Nested JSON Example

JSON objects can contain other objects and arrays.

{
    "employee": {
        "id": 1,
        "name": "John"
    },
    "skills": [
        "JavaScript",
        "Node.js",
        "SQL"
    ]
}

JSON vs JavaScript Object

Although JSON looks similar to JavaScript objects, there are important differences.

JSONJavaScript Object
Keys must be enclosed in double quotesKeys may or may not use quotes
Stored as textStored as an object in memory
Used for data exchangeUsed within JavaScript programs
Does not support functionsCan contain functions

Example JSON:

{
    "name": "John"
}

Equivalent JavaScript object:

const user = {
    name: "John"
};

Common Uses of JSON

JSON is widely used for:

  • REST API requests

  • REST API responses

  • Configuration files

  • Database exports

  • Data storage

  • Web applications

  • Mobile applications

  • Automation testing


Real-World Example

A configuration file.

{
    "browser": "chrome",
    "headless": true,
    "baseUrl": "https://example.com"
}

Applications can read this file to determine how they should run.


Automation Testing Example

JSON is heavily used in automation frameworks.

Playwright Configuration

{
    "browser": "chromium",
    "headless": true
}

Selenium Test Data

{
    "username": "admin",
    "password": "admin123"
}

Cypress Fixture

{
    "product": "Laptop",
    "price": 75000
}

API Request Payload

{
    "name": "John",
    "email": "john@example.com"
}

API Response

{
    "status": 200,
    "message": "User created successfully."
}

Common Mistakes

Using Single Quotes

Incorrect:

{
    'name': 'John'
}

Correct:

{
    "name": "John"
}

Missing Commas

Incorrect:

{
    "name": "John"
    "age": 25
}

Correct:

{
    "name": "John",
    "age": 25
}

Adding Trailing Commas

Incorrect:

{
    "name": "John",
}

Correct:

{
    "name": "John"
}

Best Practices

  • Always use double quotes for keys and string values.

  • Use meaningful key names.

  • Keep JSON properly formatted and indented.

  • Avoid unnecessary nesting.

  • Validate JSON before using it.

  • Store configuration data in JSON files.

  • Use consistent naming conventions across JSON documents.


Conclusion

JSON is one of the most important data formats in modern software development. Its simple structure, readability, and cross-platform compatibility make it the preferred choice for exchanging data between systems.

For automation engineers, understanding JSON is essential because it is used extensively in API testing, configuration files, test data management, and automation frameworks.

Mastering JSON basics provides a strong foundation for working with Node.js applications and modern web services.


Frequently Asked Questions (FAQs)

What does JSON stand for?

JSON stands for JavaScript Object Notation.


Is JSON only used with JavaScript?

No. JSON is language-independent and is supported by most programming languages.


What is the difference between JSON and a JavaScript object?

JSON is a text format used for data exchange, while a JavaScript object exists in memory and can contain functions and other JavaScript features.


Why are double quotes required in JSON?

According to the JSON specification, object keys and string values must use double quotes.


Why is JSON important in automation testing?

Automation engineers use JSON for API request payloads, API responses, configuration files, test data, fixtures, and execution settings.


Key Takeaways

  • JSON stands for JavaScript Object Notation.

  • JSON is a lightweight, text-based data format.

  • JSON stores data using key-value pairs.

  • JSON supports strings, numbers, booleans, null, objects, and arrays.

  • JSON is widely used for APIs, configuration files, and data storage.

  • JSON is different from JavaScript objects, although they have similar syntax.

  • Always use double quotes in JSON.

  • JSON is extensively used in Playwright, Selenium, Cypress, API testing, and Node.js applications.

  • Understanding JSON is essential for backend development and automation testing.

  • Mastering JSON basics prepares you to work with APIs and modern web applications.