json-file-plus
DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/json-file-plus package

4.0.0 • Public • Published

json-file-plus Version Badge

github actions coverage dependency status dev dependency status License Downloads

npm badge

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 filename = path.join(process.cwd(), 'package.json');

jsonFile(filename).then((file) => {
	file.data; // Direct access to the data from the file
	file.format; // extracted formatting data. change at will.

	file.get('version'); // get top-level keys. returns a Promise
	file.get(); // get entire data. returns a Promise

	/* pass any plain object into "set" to merge in a deep copy */
	/* please note: references will be broken. */
	/* if a non-plain object is passed, will throw a TypeError. */
	file.set({
		foo: 'bar',
		bar: {
			baz: true,
		},
	});

	file.remove('description'); // remove a specific key-value pair. returns a Promise

	/* change the filename if desired */
	file.filename = path.join(process.cwd(), 'new-package.json');

	/* Save the file, preserving formatting. returns a Promise. */
	file.save().then(function () {
		console.log('success!');
	}).catch(function (err) {
		console.log('error!', err);
	});
});

Tests

Simply run npm test in the repo

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
4.0.013latest

Version History

VersionDownloads (Last 7 Days)Published
4.0.013
3.3.221,125
3.3.177,236
3.3.056
3.2.0230
3.1.04
3.0.14
3.0.07
1.0.66
1.0.53
1.0.43
1.0.34
1.0.23
1.0.06
0.2.24
0.2.14
0.2.04
0.1.03
0.0.73
0.0.65
0.0.54
0.0.43
0.0.33
0.0.23
0.0.14

Package Sidebar

Install

npm i json-file-plus

Weekly Downloads

98,769

Version

4.0.0

License

MIT

Unpacked Size

18.2 kB

Total Files

7

Last publish

Collaborators

  • ljharb