What is hjson?
Hjson is a user interface for JSON. It allows you to write JSON in a more human-friendly format, making it easier to read and edit. Hjson supports comments, multi-line strings, and other features that make JSON more readable and maintainable.
What are hjson's main functionalities?
Parsing Hjson to JSON
This feature allows you to parse Hjson formatted strings into standard JSON objects. The code sample demonstrates how to convert a Hjson string with comments into a JSON object.
const Hjson = require('hjson');
const hjsonData = `{
// This is a comment
key: "value"
}`;
const jsonData = Hjson.parse(hjsonData);
console.log(jsonData);
Stringifying JSON to Hjson
This feature allows you to convert JSON objects into Hjson formatted strings. The code sample shows how to stringify a JSON object into a Hjson string with all keys quoted.
const Hjson = require('hjson');
const jsonData = { key: "value" };
const hjsonData = Hjson.stringify(jsonData, { quotes: 'all' });
console.log(hjsonData);
Reading Hjson from a file
This feature allows you to read Hjson data from a file and parse it into a JSON object. The code sample demonstrates reading a Hjson file and converting its content to a JSON object.
const Hjson = require('hjson');
const fs = require('fs');
const hjsonData = fs.readFileSync('data.hjson', 'utf8');
const jsonData = Hjson.parse(hjsonData);
console.log(jsonData);
Writing Hjson to a file
This feature allows you to write Hjson formatted strings to a file. The code sample shows how to convert a JSON object to a Hjson string and write it to a file.
const Hjson = require('hjson');
const fs = require('fs');
const jsonData = { key: "value" };
const hjsonData = Hjson.stringify(jsonData, { quotes: 'all' });
fs.writeFileSync('data.hjson', hjsonData);
console.log('Hjson data written to file');
Other packages similar to hjson
json5
JSON5 is an extension of JSON that aims to make JSON more human-friendly. It allows comments, trailing commas, and more. Compared to Hjson, JSON5 is closer to standard JSON but still offers some of the readability improvements.
yaml
YAML is a human-readable data serialization standard that can be used in conjunction with all programming languages. It is more flexible and readable than JSON but can be more complex. YAML supports comments, multi-line strings, and more, similar to Hjson.
toml
TOML is a data serialization language designed to be a minimal configuration file format that's easy to read due to its simplicity. It supports comments and is more readable than JSON, similar to Hjson.
hjson-js
HJSON reference parser implementation.
HJSON is JSON for humans.
That means that you can write:
{
# look, no quotes or commas!
foo: Hello World!
bar: Hello HJSON!
}
instead of:
{
"foo": "Hello World!",
"bar": "Hello HJSON!"
}
For details see http://laktak.github.io/hjson.
Install from npm
npm install hjson
Usage
var HJSON = require('hjson');
var obj = HJSON.parse(hjsonText);
var text2 = HJSON.stringify(obj);
From the Commandline
Install with npm install hjson -g
.
usage: hjson [-json] [INPUT]
hjson can be used to convert JSON from/to HJSON.
hjson will read the given JSON/HJSON input file or read from stdin.
- without options: will output as HJSON.
- with -json: will output as formatted JSON.
- with -json=compact: will output as JSON.
Sample:
- run
hjson test.json > test.hjson
to convert to HJSON - run
hjson -json test.hjson > test.json
to convert to JSON