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.parse
method.
Install the gem and add to the application's Gemfile by executing:
bundle add jsonc
If bundler is not being used to manage dependencies, install the gem by executing:
gem install jsonc
Use 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"]
# => Jules
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.