CopyPastor

Detecting plagiarism made easy.

Score: 1; Reported for: Exact paragraph match Open both answers

Possible Plagiarism

Plagiarized on 2020-05-26
by Charanjeet Singh

Original Post

Original - Posted on 2014-09-07
by sampathsris



            
Present in both answers; Present only in the new answer; Present only in the old answer;

Parsing a file containing J SON data You'll have to do some file operations with fs module.I think this will help you parse the data over
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 can use [`JSON.parse()`][1]**.
You should be able to use the `JSON` object on any [ECMAScript 5][2] compatible JavaScript implementation. And [V8][3], 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: https://devcenter.heroku.com/articles/config-vars#setting-up-config-vars-for-a-deployed-application. 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`][4]: 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][9]. 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`][6] 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][7] 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][8], 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.
[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse [2]: http://es5.github.com/#x15.12 [3]: https://developers.google.com/v8/ [4]: https://stackoverflow.com/a/7165572/1461424 [6]: https://www.npmjs.org/package/load-json-file [7]: https://www.npmjs.com/search?q=json%20parse%20stream [8]: http://json.org/ [9]: https://stackoverflow.com/questions/5726729/how-to-parse-json-using-node-js/25710749#comment43497504_9804910

        
Present in both answers; Present only in the new answer; Present only in the old answer;