Coder Perfect

How do I use Node.js to parse JSON? [closed]


How should I use Node.js to parse JSON? Is there a module that securely validates and parses JSON?

Asked by Tikhon Jelvis

Solution #1

You may just use JSON.parse to parse JSON data.

The ECMAScript 5 specification includes a definition for the JSON object. The V8 engine in Google Chrome adheres to the ECMA standard, and node.js is built on it. As a result, node.js contains a global object called JSON[docs].

Because JSON.parse is a synchronous operation, it may snarl the current thread. If you want to process large JSON objects, you should use a streaming json parser.

Answered by Felix Kling

Solution #2

You have the option to request. files in the json format

var parsedJSON = require('./file-name');

If your source code file is in the same directory as your config.json file, for example, you would use:

var config = require('./config.json');

or (you can omit the file extension):

var config = require('./config');

Note that require is synchronous and reads the file only once; subsequent calls return the cached result.

Also, because it executes any code within the file, you should only use this for local files under your complete control.

Answered by eliocs

Solution #3

You can use JSON.parse to parse JSON data ().

The JSON object should work with any JavaScript implementation that supports ECMAScript 5. V8, the foundation of Node.js, is one of them.

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

You’ll need to use the fs module to do some file operations.

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);
var fs = require('fs');
var json = JSON.parse(fs.readFileSync('/path/to/file.json', 'utf8'));

You can use require: on occasion.

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

However, I do not suggest it for a number of reasons:

Seriously! Use JSON.parse.

It becomes tedious to write boilerplate code every time you read a big number of.json files (or if you are particularly lazy). Using the load-json-file module, you can save certain characters.

const loadJsonFile = require('load-json-file');
loadJsonFile('/path/to/file.json').then(json => {
    // `json` contains the parsed object
let obj = loadJsonFile.sync('/path/to/file.json');

A streaming JSON parser is required if the JSON material is broadcast over the network. Otherwise, until JSON content is fully transmitted, your processor will be clogged and your event loop will be choked.

There are several packages in NPM that can help you with this. Choose the one that is most beneficial to you.

If you’re not sure if the data supplied to JSON.parse() is acceptable JSON, use a try/catch block to protect the call to JSON.parse(). A user-supplied JSON string can cause your application to crash and potentially expose security vulnerabilities. If you’re parsing JSON from the outside, make sure you handle errors.

Answered by sampathsris

Solution #4

make use of a JSON object:


Answered by Nobody

Solution #5

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);


Answered by pyprism

Post is based on