Sometimes the JSON that’s received from a web service is hard to read. Ruby’s JSON module comes with a function to help:

require 'json'

json = JSON.parse(raw_text)
puts JSON.pretty_generate(json)

The pretty_generate function will add line breaks where needed and fix any alignment issues.

Adding in syntax highlighting using rouge requires only a few more steps:

require 'json''
require 'rouge'

def prettify_json(raw_text)
  json = JSON.parse(raw_text)

def highlight_json(text)
  formatter = 'highlight')
  lexer = Rouge::Lexer.find('json')

Calling highlight_json will return HTML markup to display json with syntax highlighting.