A Node.js module for reading/modifying/writing JSON files. Based on json-file.
$ npm install --save jsonfile2
var json = require('jsonfile2');
// Load a JSON file
var file = json.read('./package.json');
// Read and write some values
file.get('version'); // eg. "1.0.0"
file.get('repository.type'); // eg. "git"
file.set('description', 'Some new description');
// The raw data
console.log(file.data);
// Write the updates to the file
file.writeSync();
A shortcut for creating a json.File
object and loading the file contents.
// This...
var file = json.read('/a/b/c');
// Is equivilent to this...
var file = new json.File('/a/b/c');
file.readSync();
// Likewise, this...
var file = json.read('/a/b/c', function() {
// ...
});
// Is equivilent to this...
var file = new json.File('/a/b/c');
file.read(function() {
// ...
});
JSON File object constructor. Takes a path to a JSON file.
var file = new json.File('/path/to/file.json');
Reads the JSON file and parses the contents.
file.read(function(err) {
// Now you can do things like use the .get() and .set() methods
});
Reads the JSON file and parses the contents synchronously.
Write the new contents back to the file.
file.write(function(err) {
// Your JSON file has been updated
});
Write the new contents back to the file synchronously.
Reads the JSON file and parses the contents, will not error if file doesn't exist. In that case file will be created upon save. All other errors will propagate.
Note: callback
function will be called within context of the jsonFile object.
file.update(function(err, save) {
// Now you can do things like use the .get() and .set() methods
this.get('a.b');
this.set('[c.d]', 25);
save();
});
Reads the JSON file and parses the contents synchronously, same as async sibling doesn't throw on non-existent file. (Will throw on all other errors).
file.updateSync();
// Now you can do things like use the .get() and .set() methods
file.get('a.b');
file.set('[c.d]', 25);
file.writeSync();
Get a value from the JSON data.
file.get('foo'); // === file.data['foo']
file.get('foo.bar.baz'); // === file.data['foo']['bar']['baz']
file.get('foo[bar.baz]'); // === file.data['foo']['bar.baz']
Delete a key from the JSON data.
file.del('foo'); // file.data['foo'] branch will be removed
file.del('foo.bar.baz'); // file.data['foo']['bar']['baz'] node will be removed
file.del('foo[bar.baz]'); // file.data['foo']['bar.baz'] node will be removed
Set a value in the JSON data.
file.set('foo', 'bar');
file.set('a.b.c', 'baz');
file.set('a[b.c]', 'baz');
The set
method returns the file object itself, so this method can be chained.
file.set('a', 'foo')
.set('b', 'bar')
.set('c', 'baz');