Easily edit a json file from the CLI or NodeJS
npm install dot-json
---
Easily edit a json file from the CLI or NodeJS.
bash
npm install -g dot-json
`
or local
`bash
npm install dot-json
`
$3
`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 Get a value from a json file by key-path
dot-json Assign a value at a key-path
dot-json --delete Delete a key by key-pathOptions:
--indent= Indent with 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
`$3
Add to .bash_profile:
`bash
alias package="dot-json package.json"
`
Use it like this:
`bash
package name "my-package"
`$3
#### 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"
}
}
``