# json-file

> A module for modifiying JSON files

Latest version **0.1.0** (published 2013-12-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install json-file
pnpm add json-file
yarn add json-file
bun add json-file
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2013-12-19 |
| First published | 2012-08-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 13 |
| Author | James Brumond |
| Maintainers | k |
| Keywords | json, file, i/o |

## Links

- npm: https://www.npmjs.com/package/json-file
- Repository: https://github.com/UmbraEngineering/json-file
- npm.io page: https://npm.io/package/json-file

## 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

- 0.1.0 (latest) — 2013-12-19
- 0.0.0 — 2012-08-28

## README

# json-file

A Node.js module for reading/modifying/writing JSON files.

## Install

```bash
$ npm install json-file
```

## Usage

```javascript
var json = require('json-file');

// 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.

```javascript
// 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.

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

#### File::read ( Function callback )

Reads the JSON file and parses the contents.

```javascript
file.read(function() {
	//
	// 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.

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

#### File::writeSync ( void )

Write the new contents back to the file synchronously.

#### File::get ( Mixed key )

Get a value from the JSON data.

```javascript
file.get('foo'); // === file.data['foo']
file.get('foo.bar.baz'); // === file.data['foo']['bar']['baz']
```

#### File::set ( Mixed key, Mixed value )

Set a value in the JSON data.

```javascript
file.set('foo', 'bar');
file.set('a.b.c', 'baz');
```

The `set` method returns the file object itself, so this method can be chained.

```javascript
file.set('a', 'foo')
    .set('b', 'bar')
    .set('c', 'baz');
```

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