JSON5

repository·main·Indexed 27 days ago

https://github.com/json5/json5

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

Tokens
2.2K
Snippets
6
Records
20
Agent score
89%

What's inside json5

  1. Install JSON5

    main

    To use JSON5 in your project, install it via npm.

    Node.js

    npm install json5

    Browsers (UMD)

    Include the script tag to create a global JSON5 variable:

    <script src="https://unpkg.com/json5@2/dist/index.min.js"></script>

    Browsers (ES Modules)

    <script type="module">
      import JSON5 from 'https://unpkg.com/json5@2/dist/index.min.mjs'
    </script>
  2. Serialize JavaScript values with JSON5.stringify()

    main

    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.
  3. Parse JSON5 strings with JSON5.parse()

    main

    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.
  4. JSON5 Syntax Features

    main

    JSON5 extends JSON with several features to make it easier to write and maintain by hand:

    • Objects: Keys can be unquoted IdentifierNames; trailing commas are allowed.
    • Arrays: Trailing commas are allowed.
    • Strings: Supports single quotes, multi-line strings (via escaping newlines), and character escapes.
    • Numbers: Supports hexadecimal (0x...), leading/trailing decimal points (.867, 867.), positive/negative infinity, NaN, and explicit plus signs (+1).
    • Comments: Supports both single-line (//) and multi-line (/* ... */) comments.
    • White Space: Allows additional whitespace characters.
    {
      // 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",
    }
  5. Convert JSON5 to JSON via CLI

    main

    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.

  6. Validate JSON5 content without outputting JSON

    main
    If you only want to check if a JSON5 file is syntactically valid without generating a JSON string, use the --validate (or -v) flag. If the file is invalid, the CLI will print the error message and exit with code 1.
  7. Use the JSON5 CLI

    main

    The CLI allows you to convert JSON5 to JSON or validate JSON5 syntax.

    Installation:

    npm install --global json5

    Usage:

    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.
  8. Parse JSON5 strings with parse()

    main
    Use the 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.
  9. Use a replacer function in stringify()

    main
    You can provide a function as the 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.