Try it yourself with our free Json Formatter tool — runs entirely in your browser, no signup needed.

How to Format JSON in Ruby

How to format JSON in Ruby

When working with JSON data in Ruby, it's essential to format it correctly to ensure readability, maintainability, and compatibility with other systems. Properly formatted JSON can also help catch errors and improve debugging. In this guide, we'll cover how to format JSON in Ruby, including a quick example, step-by-step breakdown, edge cases, common mistakes, performance tips, and frequently asked questions.

Quick Example

Here's a minimal example that formats a JSON string in Ruby:

require 'json'

json_string = '{"name":"John","age":30,"city":"New York"}'
formatted_json = JSON.pretty_generate(JSON.parse(json_string))

puts formatted_json

This code will output:

{
  "name": "John",
  "age": 30,
  "city": "New York"
}

Step-by-Step Breakdown

Let's walk through the code line by line:

  1. require 'json': We load the json library, which is part of the Ruby Standard Library.
  2. json_string = '{"name":"John","age":30,"city":"New York"}': We define a JSON string that we want to format.
  3. JSON.parse(json_string): We parse the JSON string into a Ruby hash using JSON.parse. This method takes a JSON string as input and returns a Ruby hash.
  4. JSON.pretty_generate(...): We use JSON.pretty_generate to format the parsed hash into a human-readable JSON string. This method takes a Ruby hash as input and returns a formatted JSON string.
  5. puts formatted_json: We print the formatted JSON string to the console.

Handling Edge Cases

Here are some common edge cases to consider when formatting JSON in Ruby:

Empty/null input

If the input JSON string is empty or null, we should handle it accordingly:

json_string = ''
formatted_json = JSON.pretty_generate(JSON.parse(json_string)) rescue nil

puts formatted_json # => nil

In this example, we use the rescue clause to catch the JSON::ParserError exception that's raised when parsing an empty string.

Invalid input

If the input JSON string is invalid, we should handle it accordingly:

json_string = '{ invalid json }'
formatted_json = JSON.pretty_generate(JSON.parse(json_string)) rescue nil

puts formatted_json # => nil

In this example, we use the rescue clause to catch the JSON::ParserError exception that's raised when parsing invalid JSON.

Large input

If the input JSON string is very large, we may need to use a streaming approach to avoid loading the entire string into memory:

require 'json/stream'

json_string = File.read('large_json_file.json')
parser = JSON::Stream::Parser.new

parser.parse(json_string) do |hash|
  formatted_json = JSON.pretty_generate(hash)
  puts formatted_json
end

In this example, we use the json/stream library to parse the large JSON string in chunks.

Unicode/special characters

If the input JSON string contains Unicode or special characters, we should ensure that they're properly encoded:

json_string = '{"name":" José"}'
formatted_json = JSON.pretty_generate(JSON.parse(json_string))

puts formatted_json

In this example, the JSON library properly encodes the Unicode character é.

Common Mistakes

Here are some common mistakes developers make when formatting JSON in Ruby:

Mistake 1: Using JSON.dump instead of JSON.pretty_generate

JSON.dump generates a compact JSON string without indentation or newlines, whereas JSON.pretty_generate generates a human-readable JSON string with indentation and newlines.

Wrong code:

json_string = '{"name":"John","age":30,"city":"New York"}'
formatted_json = JSON.dump(JSON.parse(json_string))

puts formatted_json # => {"name":"John","age":30,"city":"New York"}

Corrected code:

json_string = '{"name":"John","age":30,"city":"New York"}'
formatted_json = JSON.pretty_generate(JSON.parse(json_string))

puts formatted_json

Mistake 2: Not handling edge cases

Failing to handle edge cases like empty or invalid input can lead to unexpected errors or behavior.

Wrong code:

json_string = ''
formatted_json = JSON.pretty_generate(JSON.parse(json_string))

puts formatted_json # => raises JSON::ParserError

Corrected code:

json_string = ''
formatted_json = JSON.pretty_generate(JSON.parse(json_string)) rescue nil

puts formatted_json # => nil

Mistake 3: Not using the json library

Using other libraries or manual string manipulation can lead to errors or inconsistencies.

Wrong code:

json_string = '{"name":"John","age":30,"city":"New York"}'
formatted_json = json_string.gsub(/"/, ' ').gsub(/,/, "\n")

puts formatted_json # => { name John age 30 city New York }

Corrected code:

json_string = '{"name":"John","age":30,"city":"New York"}'
formatted_json = JSON.pretty_generate(JSON.parse(json_string))

puts formatted_json

Performance Tips

Here are some performance tips for formatting JSON in Ruby:

  • Use the json library, which is optimized for performance and security.
  • Avoid using JSON.dump and instead use JSON.pretty_generate for human-readable output.
  • Use a streaming approach for large input JSON strings.

FAQ

Q: What is the difference between JSON.dump and JSON.pretty_generate?

A: JSON.dump generates a compact JSON string without indentation or newlines, whereas JSON.pretty_generate generates a human-readable JSON string with indentation and newlines.

Q: How do I handle edge cases like empty or invalid input?

A: Use the rescue clause to catch JSON::ParserError exceptions and handle them accordingly.

Q: What is the best way to format large input JSON strings?

A: Use a streaming approach with the json/stream library to avoid loading the entire string into memory.

Q: How do I ensure that Unicode or special characters are properly encoded?

A: The JSON library properly encodes Unicode or special characters by default.

Q: What is the performance impact of using JSON.pretty_generate?

A: JSON.pretty_generate is generally slower than JSON.dump due to the additional formatting overhead. However, the performance impact is typically negligible for most use cases.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp