Playwright Read JSON File

Profile picture for user arilio666
Submitted by arilio666 on

To read the contents of a JSON file in Playwright, you'll need to utilize Node.js and the built-in fs module. 
Here is the JSON file.

const filePath = 'D:/iVagus/Playwright/user.json';
 const fileContents = fs.readFileSync(filePath, 'utf-8');
 const jsonData = JSON.parse(fileContents);

The code above uses fs.readFileSync to read the file synchronously, assumes the file is encoded in UTF-8, and parses the JSON contents with JSON.parse. 
You can conduct any additional processes after reading the JSON data.

const userData = JSON.parse(JSON.stringify(require("D:/iVagus/Playwright/user.json")))

We can also import the JSON file like the above and use it to extract information from the variable.