Skip to content

Instantly share code, notes, and snippets.

@timgentry
Created January 14, 2020 09:09
Show Gist options
  • Save timgentry/aad01c698c9e6162d708858b1430fa3c to your computer and use it in GitHub Desktop.
Save timgentry/aad01c698c9e6162d708858b1430fa3c to your computer and use it in GitHub Desktop.
Use of JSON::State to output more human readable JSON
require 'json'
hash = { 'a' => 1, 'b' => 2 }
state = JSON::State.new(indent: ' ',
space: ' ',
space_before: '',
object_nl: "\n",
array_nl: "\n")
hash.to_json # => "{\"a\":1,\"b\":2}"
hash.to_json(state) # => "{\n \"a\": 1,\n \"b\": 2\n}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment