json-file-plus 


A module to read from and write to JSON files, without losing formatting, to minimize diffs.
Example
const jsonFile = require('json-file-plus');
const path = require('path');
const fs = require('fs');
const assert = require('assert');
const filename = path.join(process.cwd(), 'package.json');
const originalContents = String(fs.readFileSync(filename));
jsonFile(filename).then((file) => {
file.data;
file.format;
file.get('version');
file.get();
file.set({
foo: 'bar',
bar: {
baz: true,
},
});
file.remove('description');
file.filename = path.join(process.cwd(), 'new-package.json');
file.save().then(() => {
console.log('success!');
const finalContents = String(fs.readFileSync(filename));
assert.equal(originalContents, finalContents);
}).catch((err) => {
console.log('error!', err);
process.exitCode = 1;
}).finally(() => {
fs.writeFileSync(filename, originalContents, { encoding: 'utf8' });
});
});
Tests
Simply run npm test in the repo