Introduction
JSON (JavaScript Object Notation) is one of the most commonly used formats for storing and exchanging structured data. In Node.js applications, JSON files are frequently used to store configuration settings, application data, user information, and API responses.
Reading JSON files allows applications to load data into memory and work with it as JavaScript objects.
For automation engineers, reading JSON files is an essential skill because automation frameworks use JSON files to store test data, configuration settings, API request payloads, expected responses, and environment variables.
In this tutorial, you’ll learn multiple ways to read JSON files in Node.js.
Why Read JSON Files?
Reading JSON files allows applications to:
Load configuration files
Read application settings
Process user information
Import product data
Read API request payloads
Load automation test data
Perform data-driven testing
Sample JSON File
Suppose employee.json contains:
{
"id": 101,
"name": "John",
"department": "IT",
"salary": 65000
}
Method 1: Read JSON Using fs.readFile()
The most common method is using the built-in File System (fs) module.
const fs = require("fs");
fs.readFile(
"employee.json",
"utf8",
(error, data) => {
if (error) {
console.log(error);
return;
}
const employee = JSON.parse(data);
console.log(employee);
}
);
Sample Output
{
id: 101,
name: 'John',
department: 'IT',
salary: 65000
}
Understanding JSON.parse()
The readFile() method returns file contents as a string.
Use JSON.parse() to convert the JSON string into a JavaScript object.
const employee = JSON.parse(data);
Now individual properties can be accessed.
console.log(employee.name);
Sample Output
John
Method 2: Read JSON Using readFileSync()
const fs = require("fs");
const data = fs.readFileSync(
"employee.json",
"utf8"
);
const employee = JSON.parse(data);
console.log(employee.department);
Sample Output
IT
Method 3: Load JSON Using require()
Node.js can directly import JSON files.
const employee = require("./employee.json");
console.log(employee.salary);
Sample Output
65000
Note:
require()caches the JSON file after the first load. If the file changes while the application is running,require()will continue returning the cached version unless the cache is cleared or the application is restarted.
Reading a JSON Array
Suppose employees.json contains:
[
{
"id": 1,
"name": "John"
},
{
"id": 2,
"name": "Alice"
},
{
"id": 3,
"name": "Bob"
}
]
const fs = require("fs");
fs.readFile(
"employees.json",
"utf8",
(error, data) => {
const employees = JSON.parse(data);
console.log(employees);
}
);
Sample Output
[
{ id: 1, name: 'John' },
{ id: 2, name: 'Alice' },
{ id: 3, name: 'Bob' }
]
Loop Through JSON Data
const fs = require("fs");
fs.readFile(
"employees.json",
"utf8",
(error, data) => {
const employees = JSON.parse(data);
employees.forEach(employee => {
console.log(employee.name);
});
}
);
Sample Output
John
Alice
Bob
Real-World Example
Read application configuration.
Suppose config.json contains:
{
"browser": "chrome",
"headless": true,
"baseUrl": "https://example.com"
}
const config = require("./config.json");
console.log(config.browser);
console.log(config.baseUrl);
Sample Output
chrome
https://example.com
Automation Testing Example
Reading JSON files is widely used in automation frameworks.
Playwright Example
Load browser configuration.
const config = require("./playwright-config.json");
console.log(config.browser);
Selenium Example
Read login credentials.
const user = require("./user.json");
console.log(user.username);
Cypress Example
Read fixture data.
const product = require("./product.json");
console.log(product.productName);
API Testing Example
Read request payload.
const payload = require("./payload.json");
console.log(payload.email);
Data-Driven Testing Example
Read employee records.
const fs = require("fs");
const employees = JSON.parse(
fs.readFileSync(
"employees.json",
"utf8"
)
);
employees.forEach(employee => {
console.log(employee.name);
});
Common Mistakes
Forgetting JSON.parse()
Incorrect:
console.log(data.name);
Since data is a string, this returns undefined.
Correct:
const employee = JSON.parse(data);
console.log(employee.name);
Invalid JSON Format
Incorrect JSON:
{
"name": "John",
}
Trailing commas are not allowed.
Correct:
{
"name": "John"
}
Forgetting Error Handling
Always check for file reading errors.
if (error) {
console.log(error);
return;
}
Best Practices
Prefer
fs.readFile()for non-blocking operations.Use
JSON.parse()after reading JSON as text.Use
require()only for static JSON files that rarely change.Validate JSON files before reading them.
Keep configuration and test data in separate JSON files.
Handle file reading errors properly.
Organize JSON files into dedicated folders such as config, data, or fixtures.
Conclusion
Reading JSON files is an essential skill for Node.js developers and automation engineers. Whether you’re loading configuration settings, reading test data, or processing API payloads, Node.js provides simple and efficient ways to work with JSON.
For automation engineers, JSON files simplify data-driven testing, configuration management, API validation, and environment setup.
Mastering JSON reading prepares you to build scalable Node.js applications and maintainable automation frameworks.
Frequently Asked Questions (FAQs)
Which module is used to read JSON files?
The built-in fs (File System) module.
Why is JSON.parse() required?
Because readFile() returns a string, JSON.parse() converts it into a JavaScript object.
Can I read JSON using require()?
Yes. Node.js allows direct importing of JSON files using require(), but the file is cached after the first load.
What is the difference between readFile() and readFileSync()?
readFile()is asynchronous and does not block execution.readFileSync()is synchronous and blocks execution until the file has been read.
Why is reading JSON important in automation testing?
Automation engineers read JSON files to load configuration settings, test data, API request payloads, expected responses, and environment-specific information.
Key Takeaways
JSON files store structured data in a lightweight format.
Use the built-in
fsmodule to read JSON files.Use
JSON.parse()to convert JSON strings into JavaScript objects.readFile()performs asynchronous file reading.readFileSync()performs synchronous file reading.require()can directly import JSON files but caches the result.Handle file reading and parsing errors properly.
JSON files are widely used in Playwright, Selenium, Cypress, API testing, and Node.js applications.
Store configuration and test data in dedicated JSON files.
Mastering JSON reading is essential for backend development and automation testing.
