# dot-json

> Easily edit a json file from the CLI or NodeJS

Latest version **1.3.0** (published 2023-05-02) · MIT license · 0 weekly downloads

## Install

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

Provides the command `dot-json`.

## 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 | 1.3.0 |
| Published | 2023-05-02 |
| First published | 2015-05-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 37.7 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 54 |
| Author | Maikel Vlasman |
| Maintainers | crobays |
| Keywords | json, edit, get, set, delete, cli |

## Links

- npm: https://www.npmjs.com/package/dot-json
- Repository: https://github.com/maikelvl/dot-json
- Issues: https://github.com/maikelvl/dot-json/issues
- npm.io page: https://npm.io/package/dot-json

## Dependencies (3)

- [docopt](https://npm.io/package/docopt.md) ~0.6.2
- [detect-indent](https://npm.io/package/detect-indent.md) ~6.0.0
- [underscore-keypath](https://npm.io/package/underscore-keypath.md) ~0.0.22

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

- 1.3.0 (latest) — 2023-05-02
- 1.2.2 — 2021-02-21
- 1.2.1 — 2020-09-11
- 1.2.0 — 2020-01-14
- 1.1.0 — 2019-01-08
- 1.0.4 — 2017-11-13
- 1.0.3 — 2015-05-14
- 1.0.2 — 2015-05-14
- 1.0.1 — 2015-05-14
- 0.1.0 — 2015-05-02

## README

# dot-json

[![huntr](https://cdn.huntr.dev/huntr_security_badge_mono.svg)](https://huntr.dev)

---

Easily edit a json file from the CLI or NodeJS.

### Install global
```bash
npm install -g dot-json
```
or local
```bash
npm install dot-json
```
### Use from the CLI
```bash
dot-json myfile.json user.name "John Doe"
dot-json myfile.json user.email "john@example.com"
dot-json myfile.json foo..bar baz
dot-json myfile.json address '{"city":"Atlantis"}' --json-value
```
myfile.json now looks like
```json
{
    "user": {
        "name": "John Doe",
        "email": "john@example.com"
    },
    "foo.bar": "baz",
    "address": {
        "city": "Atlantis"
    }
}
```

```bash
dot-json myfile.json user.name
John Doe
```
```
Usage:
  dot-json <file> <key-path>             Get a value from a json file by key-path
  dot-json <file> <key-path> <value>     Assign a value at a key-path
  dot-json <file> <key-path> --delete    Delete a key by key-path

Options:
  --indent=<n>      Indent with <n> of white space characters [default: auto] [--json-value]
  -d --delete       Delete the key-path
  -j --json-value   Parse the input value as a JSON string (to set whole objects or arrays)
  -h --help         Show this message with options
  -v --version      Print the version number
```

### Quick tip for editing package.json
Add to .bash_profile:
```bash
alias package="dot-json package.json"
```
Use it like this:
```bash
package name "my-package"
```

### Use it in NodeJS
#### Initialization
```javascript
var DotJson = require('dot-json');
var myfile = new DotJson('myfile.json');
```

#### Writing
asynchronous
```javascript
myfile.set('user.name', 'John Doe').set('user.email', 'john@example.com').save(function(){
  console.log('saved');
});
```

synchronous
```javascript
myfile.set('user.name', 'John Doe').set('user.email', 'john@example.com').save();
```

myfile.json now looks like
```json
{
    "user": {
        "name": "John Doe",
        "email": "john@example.com"
    }
}
```

#### Reading
asynchronous
```javascript
myfile.get('user.name', function(value){
  // value = 'John Doe'
  console.log(value);
});
```

synchronous
```javascript
var value = myfile.get('user.name');
// value = 'John Doe'
console.log(value);
```

#### Deleting
asynchronous
```javascript
myfile.delete('user.name').save(function(){
  console.log('saved');
});
```

synchronous
```javascript
myfile.delete('user.name').save();
```

myfile.json now looks like
```json
{
    "user": {
        "email": "john@example.com"
    }
}
```

[npmjs.org/package/dot-json](https://npmjs.org/package/dot-json)

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