npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jsonfile2

v2.1.0

Published

A module for modifying JSON files (extended from npm.org/json-file)

Downloads

4

Readme

jsonfile2

A Node.js module for reading/modifying/writing JSON files. Based on json-file.

Build Status

Install

$ npm install --save jsonfile2

Usage

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();

API

json.read ( String filePath[, Function callback ])

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 ( String filePath )

JSON File object constructor. Takes a path to a JSON file.

var file = new json.File('/path/to/file.json');

File::read ( Function callback )

Reads the JSON file and parses the contents.

file.read(function(err) {
  // Now you can do things like use the .get() and .set() methods
});

File::readSync ( void )

Reads the JSON file and parses the contents synchronously.

File::write ( Function callback )

Write the new contents back to the file.

file.write(function(err) {
  // Your JSON file has been updated
});

File::writeSync ( void )

Write the new contents back to the file synchronously.

File::update ( Function callback )

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();
});

File::updateSync ( void )

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();

File::get ( Mixed key )

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']

File::del ( Mixed key )

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

File::set ( Mixed key, Mixed value )

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');