What is cjson?
The cjson npm package is a JSON parser that supports comments and more relaxed JSON syntax. It allows you to parse JSON files that include comments, trailing commas, and other non-standard JSON features.
What are cjson's main functionalities?
Parse JSON with comments
This feature allows you to parse JSON strings that include comments. The comments are ignored during parsing, making it easier to include explanations or notes within your JSON files.
const cjson = require('cjson');
const jsonData = cjson.parse('{"key": "value" // This is a comment
}');
console.log(jsonData);
Stringify JSON with comments
This feature allows you to convert a JavaScript object into a JSON string while preserving comments. This can be useful for generating JSON files that include comments for documentation purposes.
const cjson = require('cjson');
const jsonObject = { key: 'value' };
const jsonString = cjson.stringify(jsonObject, null, 2, { comments: true });
console.log(jsonString);
Load JSON file with comments
This feature allows you to load and parse a JSON file that includes comments. The comments are ignored during parsing, making it easier to maintain and understand your JSON files.
const cjson = require('cjson');
const jsonData = cjson.load('path/to/your/file.json');
console.log(jsonData);
Other packages similar to cjson
json5
JSON5 is a JSON parser that allows for more relaxed JSON syntax, including comments, trailing commas, and unquoted keys. It is similar to cjson in that it supports non-standard JSON features, but JSON5 is more widely adopted and has a more extensive feature set.
hjson
Hjson is a user interface for JSON that allows for a more human-readable and writable format. It supports comments, multiline strings, and other features that make JSON easier to work with. Hjson is similar to cjson in that it supports comments and relaxed syntax, but it also offers additional features for improving readability.
comment-json
comment-json is a JSON parser and stringifier that supports comments. It allows you to parse and stringify JSON with comments, similar to cjson. However, comment-json focuses specifically on handling comments and may be simpler to use for that specific purpose.
JSON has a good spec, is implemented in every language, has easy to read syntax and is much more powerful than ini files.
JSON is perfect for writing config files, except of one problem - there is no comments, but sometimes config files get large and need to be commented.
Well, you could just evaluate json file as a JavaScript using one-liner, right?
The purpose of this module is to avoid dirty JavaScript configs and to enable clean, consistent, secure, portable and JSON valid notation.
CJSON supports JavaScript style comments: singleline "//" and multiline "/**/". It takes care about comments inside of strings.
Example of such shiny config file:
{
"host": "localhost",
"port": 8888
}
API
load the module
var cjson = require('cjson');
cjson.load(path, [options]);
Load config file from given path, array of paths or directory. Second parameter is optional and can be a boolean or object.
path
{String|Array} absolute path to the file, array of paths or directoryoptions
{Boolean|Object} optional options. If you pass true
as second param, its the same like {merge: true}
and will merge all configs together.
options
defaults:
{
merge: false,
replace: null,
freeze: false,
ext: '.json',
parse: jph.parse
}
Examples:
var conf = cjson.load('/path/to/your/config.json');
var conf = cjson.load(['/path/to/your/config1.json', '/path/to/your/config2.json']);
{
config1: {key1: 'value1'}
config2: {key2: 'value2'}
}
var conf = cjson.load(['/path/to/your/config1.json', '/path/to/your/config2.json'], true);
{
key1: 'value1',
key2: 'value2'
}
var conf = cjson.load('/path/to/your/configs');
var paths = ['/path/to/conf.json'];
if (process.env.NODE_ENV ==='production') {
paths.push('/path/to/conf-prod.json');
}
var conf = cjson.load(paths, true);
cjson.extend([deep], target, object1, [objectN])
Merge the contents of two or more objects together into the first object.
deep
If true, the merge becomes recursive.target
The object to extend. It will receive the new properties.object1
An object containing additional properties to merge in.objectN
Additional objects containing properties to merge in.
Example:
var object = cjson.extend({}, object1, object2);
Remove JavaScript style comments, singleline - '//' and multiline - '/**/'. It takes care
about comments inside of strings and escaping.
cjson.parse(str, [reviver])
Like JSON.parse
, but it takes care about comments. Optional reviver
argument
is for JSON.parse
method and will be called for every key and value at every level
of the final result
cjson.replace(str, obj)
Replace all strings {{key}}
contained in {key: 'value'}
, where key
can be any
property of passed obj
.
Example:
var str = '{"path": "{{root}}/src"}';
cjson.replace(str, {root: '/usr'});
cjson.freeze(obj)
Recursively freeze an object.
Installation
npm install cjson