# @exponent/json-file

> A module for reading, writing, and manipulating JSON files

Latest version **5.3.0** (published 2017-01-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install @exponent/json-file
pnpm add @exponent/json-file
yarn add @exponent/json-file
bun add @exponent/json-file
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 5.3.0 |
| Published | 2017-01-02 |
| First published | 2015-05-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 11 |
| Author | support@exponentjs.com |
| Maintainers | brentvatne, ccheever, dikaiosune, exponent, ide, jesseruder |
| Keywords | json |

## Links

- npm: https://www.npmjs.com/package/@exponent/json-file
- Repository: https://github.com/exponentjs/json-file
- Homepage: https://github.com/exponentjs/json-file#readme
- Issues: https://github.com/exponentjs/json-file/issues
- npm.io page: https://npm.io/package/@exponent/json-file

## Dependencies (3)

- [mz](https://npm.io/package/mz.md) ^2.6.0
- [json5](https://npm.io/package/json5.md) ^0.5.0
- [lodash](https://npm.io/package/lodash.md) ^4.6.1

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 5.3.0 (latest) — 2017-01-02
- 5.2.0 — 2016-06-22
- 5.1.0 — 2016-06-21
- 5.0.1 — 2016-04-26
- 5.0.0 — 2016-03-30
- 4.0.0 — 2016-03-29
- 3.0.0 — 2015-06-18
- 2.7.0 — 2015-06-13
- 2.6.0 — 2015-06-12
- 2.5.0 — 2015-05-16
- 2.3.3 — 2015-05-16
- 2.3.2 — 2015-05-16
- 2.3.1 — 2015-05-16
- 2.3.0 — 2015-05-16
- 2.2.1 — 2015-05-14
- … 5 more at https://npm.io/package/@exponent/json-file/versions

## README

# json-file
A module for reading, writing, and manipulating JSON files

## Importing the package
```js
import JsonFile from '@exponent/json-file';
```

## Promise-based async API

Everything returns `Promise`s. If you are using ES7 (or Babel), you can write code like this:
```js
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.
```js
JsonFile.readAsync('config.json', {cantReadFileDefault: {}}).then(config => {
   ...
});
```

## Used as an object
```js

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

## Used as functions
```js

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.

---
_Source: https://npm.io/package/@exponent/json-file · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
