Skip to content

JSON5

JSON5 is a superset of the JSON (JavaScript Object Notation) data interchange format, designed to be more human-readable and developer-friendly while maintaining compatibility with standard JSON parsers. JSON5 adds several additional features and syntax enhancements to JSON, making it easier to work with for developers. Some of the key features of JSON5 include:

  • Comments: JSON5 allows both single-line (using //) and multi-line (using /* */) comments within JSON documents. Comments can be useful for documenting code, providing context, or temporarily disabling certain parts of a JSON file.

  • Trailing Commas: JSON5 allows trailing commas in arrays and objects. This means you can add a comma after the last element in an array or object without causing a syntax error. Trailing commas can help simplify code formatting and reduce errors when modifying JSON data.

  • Single-Quoted Strings: In addition to double-quoted strings, JSON5 allows single-quoted strings. This can be useful when working with JSON data that contains double quotes and makes it easier to represent string literals without escaping characters.

  • Unquoted Object Keys: JSON5 permits unquoted object keys if they consist of valid JavaScript identifier names. This allows developers to write object keys without enclosing them in quotes, similar to JavaScript object literals.

  • Multi-line Strings: JSON5 supports multi-line strings enclosed in backticks (`), similar to template literals in JavaScript. Multi-line strings make it easier to represent long strings without concatenation or escaping line breaks.

  • Hexadecimal, Octal, and Binary Numbers: JSON5 extends the numeric format to include hexadecimal (0x prefix), octal (0o prefix), and binary (0b prefix) literals, in addition to decimal numbers. This can be useful for representing numeric values in different bases.

JSON5 aims to provide a more flexible and expressive data format while remaining compatible with existing JSON parsers. It's particularly useful in scenarios where human readability and developer convenience are important, such as configuration files, data interchange formats, and JavaScript-based applications.

Here's an example of JSON5 syntax:

json5
{
    // Single-line comment
    key: 'value', // Trailing comma
    
    // Multi-line string
    description: `This is a
                  multi-line string`,
    
    // Hexadecimal number
    hexValue: 0xFF,
    
    // Object with unquoted keys
    nestedObject: {
        nestedKey: 'nestedValue'
    }
}

JSON5 is not a separate standard but rather an extension of JSON, so JSON5 documents are also valid JSON documents. However, not all JSON parsers support JSON5 features, so it's important to ensure compatibility with the target environment when using JSON5.