prettify_json
is a powerful command-line tool designed to enhance the readability of JSON data. Whether you're working with JSON files, strings, or data streamed from the web, prettify_json
will format it in a clean, colorful, and human-friendly way.
- Readability: Formats JSON data for easier reading.
- Flexibility: Accepts input from files, strings, or standard input.
- Colorful Output: Adds colors to the JSON keys, strings, numbers, and more for better visual distinction.
- Ruby (version 2.7.0 or higher)
Add this line to your application's Gemfile:
gem 'prettify_json'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install prettify_json
require 'prettify_json'
# Sample JSON string
json_string = '{"name": "John Doe", "age": 30, "is_student": false, "address": null, "scores": [95, 85, 92.5]}'
# Create an instance of JsonPrettifier
prettifier = PrettifyJson::JsonPrettifier.new(json_string)
# Get the prettified JSON
prettified_json = prettifier.pretty_print
# Output the prettified JSON
puts prettified_json
Prettify JSON data from a file:
$ prettify_json -f path/to/your/file.json
Prettify JSON data from a string:
$ prettify_json -s '{"key": "value"}'
Prettify JSON data from standard input:
$ echo '{"key": "value"}' | prettify_json -
Fetch and prettify JSON data from a web API:
$ curl -s https://api.restful-api.dev/objects | prettify_json -
$ prettify_json -f sample.json
{
"name": "John Doe",
"age": 30,
"city": "New York"
}
$ prettify_json -s '{"name": "John Doe", "age": 30, "city": "New York"}'
{
"name": "John Doe",
"age": 30,
"city": "New York"
}
$ curl -s https://api.restful-api.dev/objects | prettify_json -
[
{
"id": 1,
"name": "Object 1",
"data": {
"value": "Sample data"
}
},
{
"id": 2,
"name": "Object 2",
"data": {
"value": "More sample data"
}
}
]
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
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
. To release a new version, update the version number in lib/prettify_json/version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and the created tag, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/talaatmagdyx/prettify_json. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
If you encounter any bugs or have suggestions for new features, please open an issue on GitHub. Your feedback is valuable and helps improve the quality of the gem.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the PrettifyJson project's codebases, issue trackers, chat rooms, and mailing lists is expected to follow the code of conduct.