How to parse JSON using Node.js ?
- node.js is built on V8, which provides the global object JSON[docs]. The definition of the JSONobject is part of the ECMAScript 5 specification.
- Note - JSON.parse can tie up the current thread because it is a synchronous method
we can require .json files.
For example if we have a
config.json file in the same directory as your source code file we would use:
or (file extension can be omitted):
Note that require is synchronous and only reads the file once, following calls return the result from cache
Parsing a string containing JSON data
Parsing a file containing JSON data
we'll have to do some file operations with fs module.
use the JSON object: