4 Easily read/write JSON files.
6 [![npm Package](https://img.shields.io/npm/v/jsonfile.svg?style=flat-square)](https://www.npmjs.org/package/jsonfile)
7 [![build status](https://secure.travis-ci.org/jprichardson/node-jsonfile.svg)](http://travis-ci.org/jprichardson/node-jsonfile)
8 [![windows Build status](https://img.shields.io/appveyor/ci/jprichardson/node-jsonfile/master.svg?label=windows%20build)](https://ci.appveyor.com/project/jprichardson/node-jsonfile/branch/master)
10 <a href="https://github.com/feross/standard"><img src="https://cdn.rawgit.com/feross/standard/master/sticker.svg" alt="Standard JavaScript" width="100"></a>
15 Writing `JSON.stringify()` and then `fs.writeFile()` and `JSON.parse()` with `fs.readFile()` enclosed in `try/catch` blocks became annoying.
22 npm install --save jsonfile
29 ### readFile(filename, [options], callback)
31 `options` (`object`, default `undefined`): Pass in any `fs.readFile` options or set `reviver` for a [JSON reviver](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse).
32 - `throws` (`boolean`, default: `true`). If `JSON.parse` throws an error, pass this error to the callback.
33 If `false`, returns `null` for the object.
37 var jsonfile = require('jsonfile')
38 var file = '/tmp/data.json'
39 jsonfile.readFile(file, function(err, obj) {
45 ### readFileSync(filename, [options])
47 `options` (`object`, default `undefined`): Pass in any `fs.readFileSync` options or set `reviver` for a [JSON reviver](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse).
48 - `throws` (`boolean`, default: `true`). If `JSON.parse` throws an error, throw the error.
49 If `false`, returns `null` for the object.
52 var jsonfile = require('jsonfile')
53 var file = '/tmp/data.json'
55 console.dir(jsonfile.readFileSync(file))
59 ### writeFile(filename, obj, [options], callback)
61 `options`: Pass in any `fs.writeFile` options or set `replacer` for a [JSON replacer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify). Can also pass in `spaces`.
65 var jsonfile = require('jsonfile')
67 var file = '/tmp/data.json'
68 var obj = {name: 'JP'}
70 jsonfile.writeFile(file, obj, function (err) {
75 **formatting with spaces:**
78 var jsonfile = require('jsonfile')
80 var file = '/tmp/data.json'
81 var obj = {name: 'JP'}
83 jsonfile.writeFile(file, obj, {spaces: 2}, function(err) {
89 ### writeFileSync(filename, obj, [options])
91 `options`: Pass in any `fs.writeFileSync` options or set `replacer` for a [JSON replacer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify). Can also pass in `spaces`.
94 var jsonfile = require('jsonfile')
96 var file = '/tmp/data.json'
97 var obj = {name: 'JP'}
99 jsonfile.writeFileSync(file, obj)
102 **formatting with spaces:**
105 var jsonfile = require('jsonfile')
107 var file = '/tmp/data.json'
108 var obj = {name: 'JP'}
110 jsonfile.writeFileSync(file, obj, {spaces: 2})
117 Global configuration to set spaces to indent JSON files.
122 var jsonfile = require('jsonfile')
126 var file = '/tmp/data.json'
127 var obj = {name: 'JP'}
129 // json file has four space indenting now
130 jsonfile.writeFile(file, obj, function (err) {
135 Note, it's bound to `this.spaces`. So, if you do this:
139 myObj.writeJsonSync = jsonfile.writeFileSync
140 // => this.spaces = null
143 Could do the following:
146 var jsonfile = require('jsonfile')
148 jsonfile.writeFileSync(file, obj) // will have 4 spaces indentation
150 var myCrazyObj = {spaces: 32}
151 myCrazyObj.writeJsonSync = jsonfile.writeFileSync
152 myCrazyObj.writeJsonSync(file, obj) // will have 32 space indentation
153 myCrazyObj.writeJsonSync(file, obj, {spaces: 2}) // will have only 2
162 Copyright 2012-2016, JP Richardson <jprichardson@gmail.com>