Load JSON5 files in Node.js using require()
mainrequire() support for .json5 files in Node.js by registering the JSON5 module first.repository·main·Indexed 27 days ago
https://github.com/json5/json5An extension of the JSON format designed to be easier for humans to write and maintain, particularly for configuration files. JSON5 is a superset of JSON that supports comments, single quotes, trailing commas, unquoted keys, and hexadecimal numbers. It provides a JavaScript library with JSON5.parse() and JSON5.stringify() methods compatible with the standard JSON API, as well as a CLI tool for validating and converting JSON5 to standard JSON.
require() support for .json5 files in Node.js by registering the JSON5 module first.Depending on your module system, import JSON5 as follows:
const JSON5 = require('json5')import JSON5 from 'json5'To use JSON5 in your project, install it via npm.
npm install json5Include the script tag to create a global JSON5 variable:
<script src="https://unpkg.com/json5@2/dist/index.min.js"></script><script type="module">
import JSON5 from 'https://unpkg.com/json5@2/dist/index.min.mjs'
</script>Converts a JavaScript value to a JSON5 string. It is compatible with the standard JSON.stringify() API.
Syntax:
JSON5.stringify(value[, replacer[, space]])
JSON5.stringify(value[, options])
Parameters:
value: The value to convert.replacer: A function that alters the stringification process, or an array of String and Number objects acting as a whitelist for properties.space: A String or Number used for indentation/whitespace. If a Number is used, it is capped at 10. If whitespace is used, trailing commas will be included in objects and arrays.options: An object containing:replacer: Same as the replacer parameter.space: Same as the space parameter.quote: A String representing the quote character to use when serializing strings.Parses a JSON5 string and constructs the corresponding JavaScript value or object. It is compatible with the standard JSON.parse() API.
Syntax:
JSON5.parse(text[, reviver])
Parameters:
text: The string to parse as JSON5.reviver: (Optional) A function that prescribes how the value originally produced by parsing is transformed before being returned.JSON5 extends JSON with several features to make it easier to write and maintain by hand:
IdentifierNames; trailing commas are allowed.0x...), leading/trailing decimal points (.867, 867.), positive/negative infinity, NaN, and explicit plus signs (+1).//) and multi-line (/* ... */) comments.{
// comments
unquoted: 'and you can quote me on that',
singleQuotes: 'I can use "double quotes" here',
lineBreaks: "Look, Mom! \\nNo \\n's!",
hexadecimal: 0xdecaf,
leadingDecimalPoint: .8675309, andTrailing: 8675309.,
positiveSign: +1,
trailingComma: 'in objects', andIn: ['arrays',],
"backwardsCompatible": "with JSON",
}stringify() will throw a TypeError with the message: Converting circular structure to JSON5.To convert a JSON5 file to a standard JSON file using the --convert (or -c) flag, provide the input filename. The tool will automatically create an output file with the same base name but with a .json extension.
Example: Converting config.json5 will produce config.json in the same directory.
--validate (or -v) flag. If the file is invalid, the CLI will print the error message and exit with code 1.The CLI allows you to convert JSON5 to JSON or validate JSON5 syntax.
Installation:
npm install --global json5Usage:
json5 [options] <file>If <file> is omitted, STDIN is used.
Options:
-s, --space: Number of spaces to indent or t for tabs.-o, --out-file [file]: Output to the specified file (defaults to STDOUT).-v, --validate: Validate JSON5 syntax without outputting JSON.-V, --version: Output version number.-h, --help: Output usage information.parse method to convert a JSON5 formatted string into a JavaScript object. JSON5 allows for features not present in standard JSON, such as comments, trailing commas, and unquoted keys.replacer argument. This function is called for every property. It receives the key and the value, and its this context is set to the holder object containing the property.