🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

json-file

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-file

A module for modifiying JSON files

latest
Source
npmnpm
Version
0.1.0
Version published
Weekly downloads
588
-12.63%
Maintainers
1
Weekly downloads
 
Created
Source

json-file

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

Install

$ npm install json-file

Usage

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.

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

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

File::read ( Function callback )

Reads the JSON file and parses the contents.

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.

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.

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.

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

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

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

Keywords

json

FAQs

Package last updated on 19 Dec 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts