json-file-plus
Read from and write to a JSON file, minimizing diffs and preserving formatting.
Last updated 7 years ago by ljharb .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install json-file-plus 
SYNC missed versions from official npm registry.

#json-file-plus Version Badge

Build Status 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

var jsonFile = require('json-file-plus');
var path = require('path'); // in node-core
var filename = path.join(process.cwd(), 'package.json');
var callback = function (err, result) { /* your code here */ };

/* Note: jsonFile also returns a Promise, if you prefer that to a Node-style callback ("errorback"). */
jsonFile(filename, function (err, file) {
	if (err) { return doSomethingWithError(err); }

	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('version', callback); // get top-level keys. calls the errorback
	file.get(); // get entire data. returns a Promise
	file.get(callback); // get entire data. calls the errorback

	/* 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
    	file.remove('description', callback); // remove a specific key-value pair. calls the errorback

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

	/* Save the file, preserving formatting. */
	/* Errorback will be passed to fs.writeFile */
	/* Returns a Promise. */
	file.save(callback).then(function () {
		console.log('success!');
	}).catch(function (err) {
		console.log('error!', err);
	});
});

Tests

Simply run npm test in the repo

Current Tags

  • 3.3.1                                ...           latest (7 years ago)

29 Versions

  • 3.3.1                                ...           7 years ago
  • 3.3.0                                ...           9 years ago
  • 3.2.0                                ...           9 years ago
  • 3.1.0                                ...           9 years ago
  • 3.0.1                                ...           9 years ago
  • 3.0.0                                ...           10 years ago
  • 2.0.0 [deprecated]           ...           10 years ago
  • 1.0.7 [deprecated]           ...           10 years ago
  • 1.0.6                                ...           10 years ago
  • 1.0.5                                ...           10 years ago
  • 1.0.4                                ...           10 years ago
  • 1.0.3                                ...           10 years ago
  • 1.0.2                                ...           10 years ago
  • 1.0.1 [deprecated]           ...           10 years ago
  • 1.0.0                                ...           10 years ago
  • 0.2.3 [deprecated]           ...           11 years ago
  • 0.2.2                                ...           11 years ago
  • 0.2.1                                ...           11 years ago
  • 0.2.0                                ...           11 years ago
  • 0.1.1 [deprecated]           ...           11 years ago
  • 0.1.0                                ...           11 years ago
  • 0.0.8 [deprecated]           ...           12 years ago
  • 0.0.7                                ...           12 years ago
  • 0.0.6                                ...           12 years ago
  • 0.0.5                                ...           12 years ago
  • 0.0.4                                ...           12 years ago
  • 0.0.3                                ...           12 years ago
  • 0.0.2                                ...           12 years ago
  • 0.0.1                                ...           12 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (5)
Dev Dependencies (10)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |