A simple Ruby gem to parse JSON with Comments (JSONC) and trailing commas.
This gem provides JSONC.parse and JSONC.load_file methods that act as drop-in replacements for the standard JSON library, with added support for modern JSON extensions.
- Comments: Parses JSON with
//single-line and/* */multi-line comments. - Trailing Commas: Parses JSON with trailing commas in objects and arrays.
- Standard Interface: Forwards all options (e.g.,
symbolize_names) to the standardJSON.parsemethod.
Install the gem and add to the application's Gemfile by executing:
bundle add jsoncIf bundler is not being used to manage dependencies, install the gem by executing:
gem install jsoncUse JSONC.parse in place of JSON.parse.
require 'jsonc'
jsonc_string = <<-JSONC
{
// The name of the user
"name": "Jules",
/*
The user's role.
Can be "agent" or "user".
*/
"role": "agent", // Trailing commas are allowed!
}
JSONC
parsed_hash = JSONC.parse(jsonc_string)
# => { "name" => "Jules", "role" => "agent" }
puts parsed_hash["name"]
# => JulesTo prevent memory exhaustion from malicious or excessively large inputs, you can set a max_bytes limit (default: 10MB):
# Default 10MB limit
JSONC.parse(jsonc_string)
# Custom size limit (50MB)
JSONC.parse(large_jsonc_string, max_bytes: 52_428_800)
# Also works with load_file (checks file size before reading)
JSONC.load_file('config.jsonc', max_bytes: 1_048_576) # 1MBExceeding the size limit raises a JSON::ParserError.
Note: The size limit applies to the raw JSONC input (including comments and whitespace) before sanitization. This means files with extensive comments count toward the limit.
Use JSONC.load_file in place of JSON.load_file.
# Given a file named 'config.jsonc' with the content from above:
require 'jsonc'
parsed_hash = JSONC.load_file('config.jsonc', symbolize_names: true)
# => { name: "Jules", role: "agent" }After checking out the repo, run bin/setup to install dependencies. Then, run bundle exec rspec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install.
Bug reports and pull requests are welcome on GitHub at https://github.com/ytkg/jsonc. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the JSONC project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.