UNPKG

797 BMarkdownView Raw
1# readJsonSync(file, [options])
2
3Reads a JSON file and then parses it into an object. `options` are the same
4that you'd pass to [`jsonFile.readFileSync`](https://github.com/jprichardson/node-jsonfile#readfilesyncfilename-options).
5
6**Alias:** `readJSONSync()`
7
8- `file` `<String>`
9- `options` `<Object>`
10
11## Example:
12
13```js
14const fs = require('fs-extra')
15
16const packageObj = fs.readJsonSync('./package.json')
17console.log(packageObj.version) // => 2.0.0
18```
19
20---
21
22`readJsonSync()` can take a `throws` option set to `false` and it won't throw if the JSON is invalid. Example:
23
24```js
25const fs = require('fs-extra')
26
27const file = '/tmp/some-invalid.json'
28const data = '{not valid JSON'
29fs.writeFileSync(file, data)
30
31const obj = fs.readJsonSync(file, { throws: false })
32console.log(obj) // => null
33```