Sync:
var fs = require('fs');
var obj = JSON.parse(fs.readFileSync('file', 'utf8'));
Async:
var fs = require('fs');
var obj;
fs.readFile('file', 'utf8', function (err, data) {
if (err) throw err;
obj = JSON.parse(data);
});
Answer from mihai on Stack Overflow Top answer 1 of 14
1596
Sync:
var fs = require('fs');
var obj = JSON.parse(fs.readFileSync('file', 'utf8'));
Async:
var fs = require('fs');
var obj;
fs.readFile('file', 'utf8', function (err, data) {
if (err) throw err;
obj = JSON.parse(data);
});
2 of 14
510
The easiest way I have found to do this is to just use require and the path to your JSON file.
For example, suppose you have the following JSON file.
test.json
{
"firstName": "Joe",
"lastName": "Smith"
}
You can then easily load this in your node.js application using require
var config = require('./test.json');
console.log(config.firstName + ' ' + config.lastName);
HeyNode
heynode.com โบ tutorial โบ readwrite-json-files-nodejs
Read/Write JSON Files with Node.js | HeyNode
In this tutorial, we are going to look at how to read and write to our customer.json file. ... Accessing files in Node is done with the native module fs, which gives you functions to watch, read, and write files along with many other tools to work with the filesystem.
Videos
10:48
Read JSON File in Rust | Parse JSON File in Rust | Rust Tutorial ...
04:04
How to load (parse) a .json file - YouTube
Node.js Beginner Series (16/26) How to read and update ...
05:29
How to Open and Read Files in Nodejs Using fs Module - YouTube
13:33
NodeJs: Reading and Writing to files - YouTube
read & write JSON in Node.js. Use JSON as a database
GitHub
github.com โบ jprichardson โบ node-fs-extra โบ blob โบ master โบ docs โบ readJson.md
node-fs-extra/docs/readJson.md at master ยท jprichardson/node-fs-extra
Reads a JSON file and then parses it into an object. ... const fs = require('fs-extra') // With a callback: fs.readJson('./package.json', (err, packageObj) => { if (err) console.error(err) console.log(packageObj.version) // => 0.1.3 }) // With ...
Author ย jprichardson
Stack Abuse
stackabuse.com โบ reading-and-writing-json-files-with-node-js
Reading and Writing JSON Files with Node.js
July 8, 2024 - To read the JSON data from the file we can use the Node.js fs module.
DEV Community
dev.to โบ tejesh โบ nodejs-read-json-file-using-require-vs-fs-module-4f94
NodeJS require() vs fs.readFileSync to read JSON files - DEV Community
May 14, 2020 - Also, do note that when using fs.readFileSync, additionally you have to do JSON.parse after reading the content. // using require directly to read json let data = require('./file.json') // using fs module to read json const fs = require('fs') let data = JSON.parse(fs.readFileSync('file.json', 'utf-8')) Subscribe ยท
GitHub
github.com โบ stdlib-js โบ fs-read-json
GitHub - stdlib-js/fs-read-json: Read a file as JSON.
var data = readJSON.sync( file, ... 'Package description: %s', data.description ); } } @stdlib/fs-read-file: read the entire contents of a file....
Author ย stdlib-js
Futurestud.io
futurestud.io โบ tutorials โบ node-js-read-a-json-file
Node.js โ Read a JSON File
The @supercharge/fs package comes with a handy Fs.readJson(path) method. the Fs.readJson method reads and parses the content of a JSON file and returns the resolved JavaScript value:
TutorialsPoint
tutorialspoint.com โบ readjson-function-in-fs-extra-nodejs
readJson() function in fs-extra - NodeJS
Create an asyncReadJsonExample.js and copy-paste the following code snippet into that file. Now, run the following command to run the following code snippet. ... const fs = require('fs-extra') // Reading JSON with a callback: fs.readJson('./package.json', (err, packageObj) => { if (err) console.error(err) console.log(packageObj.version) // => 1.0.0 }) // Reading JSON Promises: fs.readJson('./package.json') .then(packageObj => { console.log(packageObj.version) // => 1.0.0 }) .catch(err => { console.error(err) }) // Reading JSON withasync/await: async function asyncReadJsonExample () { try { const packageObj = await fs.readJson('./package.json') console.log(packageObj.version) // => 1.0.0 } catch (err) { console.error(err) } } asyncReadJsonExample()
CodeSignal
codesignal.com โบ learn โบ courses โบ hierarchical-and-structured-data-formats-in-ts โบ lessons โบ parsing-json-files-in-typescript-using-nodejs
Parsing JSON Files in TypeScript Using Node.js
The fs.readFile function reads the file with UTF-8 encoding, and the callback function handles errors and data processing. ... To parse the JSON data, we use TypeScript's JSON.parse() function, adding type definitions for clarity and safety.
npm
npmjs.com โบ package โบ jsonfile
jsonfile - npm
const jsonfile = require('jsonfile') const file = '/tmp/data.json' jsonfile.readFile(file) .then(obj => console.dir(obj)) .catch(error => console.error(error)) options (object, default undefined): Pass in any fs.readFileSync options or set reviver for a JSON reviver.
ยป npm install jsonfile
Published ย Aug 12, 2025
Version ย 6.2.0
Author ย JP Richardson
Repository ย https://github.com/jprichardson/node-jsonfile