How to parse JSON using Node.js?


How should I parse JSON using Node.js? Is there some module which will validate and parse JSON securely?

8/3/2016 10:22:11 AM

Accepted Answer

You can simply use JSON.parse.

The definition of the JSON object is part of the ECMAScript 5 specification. node.js is built on Google Chrome's V8 engine, which adheres to ECMA standard. Therefore, node.js also has a global object JSON[docs].

Note - JSON.parse can tie up the current thread because it is a synchronous method. So if you are planning to parse big JSON objects use a streaming json parser.

9/17/2018 12:34:22 PM

You can use JSON.parse().

You should be able to use the JSON object on any ECMAScript 5 compatible JavaScript implementation. And V8, upon which Node.js is built is one of them.

Note: If you're using a JSON file to store sensitive information (e.g. passwords), that's the wrong way to do it. See how Heroku does it: Find out how your platform does it, and use process.env to retrieve the config vars from within the code.

Parsing a string containing JSON data

var str = '{ "name": "John Doe", "age": 42 }';
var obj = JSON.parse(str);

Parsing a file containing JSON data

You'll have to do some file operations with fs module.

Asynchronous version

var fs = require('fs');

fs.readFile('/path/to/file.json', 'utf8', function (err, data) {
    if (err) throw err; // we'll not consider error handling for now
    var obj = JSON.parse(data);

Synchronous version

var fs = require('fs');
var json = JSON.parse(fs.readFileSync('/path/to/file.json', 'utf8'));

You wanna use require? Think again!

You can sometimes use require:

var obj = require('path/to/file.json');

But, I do not recommend this for several reasons:

  1. require is synchronous. If you have a very big JSON file, it will choke your event loop. You really need to use JSON.parse with fs.readFile.
  2. require will read the file only once. Subsequent calls to require for the same file will return a cached copy. Not a good idea if you want to read a .json file that is continuously updated. You could use a hack. But at this point, it's easier to simply use fs.
  3. If your file does not have a .json extension, require will not treat the contents of the file as JSON.

Seriously! Use JSON.parse.

load-json-file module

If you are reading large number of .json files, (and if you are extremely lazy), it becomes annoying to write boilerplate code every time. You can save some characters by using the load-json-file module.

const loadJsonFile = require('load-json-file');

Asynchronous version

loadJsonFile('/path/to/file.json').then(json => {
    // `json` contains the parsed object

Synchronous version

let obj = loadJsonFile.sync('/path/to/file.json');

Parsing JSON from streams

If the JSON content is streamed over the network, you need to use a streaming JSON parser. Otherwise it will tie up your processor and choke your event loop until JSON content is fully streamed.

There are plenty of packages available in NPM for this. Choose what's best for you.

Error Handling/Security

If you are unsure if whatever that is passed to JSON.parse() is valid JSON, make sure to enclose the call to JSON.parse() inside a try/catch block. A user provided JSON string could crash your application, and could even lead to security holes. Make sure error handling is done if you parse externally-provided JSON.


use the JSON object:


Another example of JSON.parse :

var fs = require('fs');
var file = __dirname + '/config.json';

fs.readFile(file, 'utf8', function (err, data) {
  if (err) {
    console.log('Error: ' + err);

  data = JSON.parse(data);


I'd like to mention that there are alternatives to the global JSON object. JSON.parse and JSON.stringify are both synchronous, so if you want to deal with big objects you might want to check out some of the asynchronous JSON modules.

Have a look:


Include the node-fs library.

var fs = require("fs");
var file = JSON.parse(fs.readFileSync("./PATH/data.json", "utf8"));

For more info on 'fs' library , refer the documentation at


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]