Socket
Socket
Sign inDemoInstall

json5

Package Overview
Dependencies
0
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    json5

Modern JSON.


Version published
Weekly downloads
71M
Maintainers
1
Created
Weekly downloads
 

Package description

What is json5?

The json5 npm package is a JSON parser and serializer that allows for comments, trailing commas, single quotes, and more. It is designed to be a more user-friendly and flexible version of JSON.

What are json5's main functionalities?

Parsing JSON5 Strings

This feature allows you to parse JSON5 strings into JavaScript objects. It supports comments, single quotes, and additional syntax that is not available in standard JSON.

{"parse": "JSON5.parse('{/*comment*/\"key\": \"value\"}')"}

Stringifying JavaScript Objects

This feature converts JavaScript objects into JSON5 strings. It can include features like trailing commas and unquoted keys, making the output more human-readable.

{"stringify": "JSON5.stringify({key: 'value'}, null, 2)"}

Other packages similar to json5

Changelog

Source

v0.0.1 [[code][c0.0.1], [diff][d0.0.1]]

This was the first implementation of this JSON5 parser.

  • Support unquoted object keys, including reserved words. Unicode characters and escape sequences sequences aren't yet supported.

  • Support single-quoted strings.

  • Support multi-line strings.

  • Support trailing commas in arrays and objects.

  • Support comments, both inline and block.

Readme

Source

JSON5 – Modern JSON

JSON is strict. Keys need to be quoted; strings can only be double-quoted; objects and arrays can't have trailing commas; and comments aren't allowed.

Using such a strict subset of "JavaScript object notation" was likely for the best at the time, but with modern ECMAScript 5 engines like V8 in Chrome and Node, these limitations are cumbersome.

JSON5 does for JSON what ES5 did for ES3. It also is to regular ES5 what JSON was to ES3 — a pure subset.

This module provides a replacement for ES5's native JSON.parse() method that understands these additions. The parser is based directly off of Douglas Crockford's json_parse.js, which avoids eval() and validates input as it parses it, making it secure and safe to use today.

Features

  • Object keys don't need to be quoted if they contain no special characters. Yes, even reserved keywords are valid unquoted keys in ES5.

    [TODO: Unicode characters and escape sequences aren't yet supported in unquoted keys.]

  • Strings can be single-quoted.

  • Strings can be multi-line; just prefix the newline with a backslash.

  • Objects and arrays can have trailing commas.

  • Both inline (single-line) and block (multi-line) comments are allowed.

  • [IDEA: Allow octal and hexadecimal numbers.]

Example

{
    foo: 'bar',
    while: true,
    
    this: 'is a\
 multi-line string',
 
    // this is an inline comment
    here: 'is another', // inline comment
    
    /* this is a block comment
       it continues on another line */
       
    finally: 'a trailing comma',
    oh: [
        'we shouldn\'t forget',
        'arrays can have',
        'trailing commas too',
    ],
}

Installation

Via npm on Node:

npm install json5
var JSON5 = require('json5');

Or in the browser (adds the JSON5 object to the global namespace):

<script src="json5.js"></script>

Usage

var obj = JSON5.parse('{unquoted:"key",trailing:"comma",}');
var str = JSON5.stringify(obj);
console.log(obj);
console.log(str);

JSON5.stringify() is currently aliased to the native JSON.stringify() in order for the output to be fully compatible with all JSON parsers today.

Development

git clone git://github.com/aseemk/json5.git
cd json5
npm link
npm test

Feel free to file issues and submit pull requests — contributions are welcome.

If you submit a pull request, please be sure to add or update corresponding test cases, and ensure that npm test continues to pass.

License

MIT License. © 2012 Aseem Kishore.

Credits

Michael Bolin independently arrived at and published some of these same ideas with awesome explanations and detail. Recommended reading: Suggested Improvements to JSON

Douglas Crockford of course designed and built JSON, but his state machine diagrams on the JSON website, as cheesy as it may sound, gave me motivation and confidence that building a new parser to implement these ideas this was within my reach! This code is also modeled directly off of Doug's open-source json_parse.js parser. I'm super grateful for that clean and well-documented code.

Keywords

FAQs

Last updated on 28 May 2012

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc