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

@exponent/json-file

v5.3.0

Published

A module for reading, writing, and manipulating JSON files

Downloads

264

Readme

json-file

A module for reading, writing, and manipulating JSON files

Importing the package

import JsonFile from '@exponent/json-file';

Promise-based async API

Everything returns Promises. If you are using ES7 (or Babel), you can write code like this:

let config = await JsonFile.readAsync('config.json', {cantReadFileDefault: {}});

If you are using ES6, you can just use the return values the way you normally would use Promises.

JsonFile.readAsync('config.json', {cantReadFileDefault: {}}).then(config => {
   ...
});

Used as an object


var file = new JsonFile('config.json', {cantReadFileDefault: {}});
var somethingSaved = await file.getAsync('somethingSaved', null);

Used as functions


var pkg = await JsonFile.readAsync('package.json');
var main = await JsonFile.getAsync('package.json', 'main', 'index.js');
...

Options you can set, and their default values

|Option | Description | Default Value| |-------|-------------|--------------| |space|How many spaces to use when pretty-printing, (0 for no pretty-printing)|2| |default|Catch-all default value for missing values, bad JSON, and files that can't be read|undefined| |jsonParseErrorDefault|The default value for when a file is read but it doesn't contain valid JSON|undefined| |cantReadFileDefault|The default value for when a file can't be read|undefined|

  • Note that if defaults are undefined, then an Error will be thrown instead of undefined being returned

Methods

.readAsync([options])

Returns the parse of the whole file as an object

.getAsync(key, [default-value], [options])

Returns a single value from a JSON file, using lodash's _.get to query the whole object.

See https://lodash.com/docs#get

.writeAsync(data, [options])

Writes out the given data to the file

.setAsync(key, val, [options])

Updates the file, inserting or updating the value for <key> with <val>

.mergeAsync(sources, [options])

Merges the values in <sources> into the object currently encoded in the file.

.deleteKeyAsync(key, [options])

Deletes a single key from the top level of the file.

Functions

The functions available all mirror the methods above but take file (filename as a string) as their first argument.