Giter Club home page Giter Club logo

dot-json's Introduction

dot-json

Build Status

Easily edit a json file from the CLI or NodeJS.

Install global

npm install -g dot-json

or local

npm install --save dot-json

Use from the CLI

dot-json myfile.json user.name "John Doe"
dot-json myfile.json user.email "[email protected]"

myfile.json now looks like

{
    "user": {
        "name": "John Doe",
        "email": "[email protected]"
    }
}
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:
  -d --delete     Delete the key-path
  -h --help       Show this message with options
  -v --version    Print the version number

Quick tip for editing package.json

Add to .bash_profile:

alias package="dot-json package.json"

Use it like this:

package name "my-package"

Use it in NodeJS

Initialization

var DotJson = require('dot-json');
var myfile = new DotJson('myfile.json');

Writing

asynchronous

myfile.set('user.name', 'John Doe').set('user.email', '[email protected]').save(function(){
  console.log('saved');
});

synchronous

myfile.set('user.name', 'John Doe').set('user.email', '[email protected]').save();

myfile.json now looks like

{
    "user": {
        "name": "John Doe",
        "email": "[email protected]"
    }
}

Reading

asynchronous

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

synchronous

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

Deleting

asynchronous

myfile.delete('user.name').save(function(){
  console.log('saved');
});

synchronous

myfile.delete('user.name').save();

myfile.json now looks like

{
    "user": {
        "email": "[email protected]"
    }
}

npmjs.org/package/dot-json

dot-json's People

Contributors

maikelvl avatar ashiknesin avatar fregante avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.