Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby JSON.pretty_generate(hash,opts) does not use opts

Tags:

json

ruby

Firsty, my references:

http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html
http://apidock.com/ruby/JSON/pretty_generate

Second my versions:

$ gem list | egrep -nir "json"
json (1.8.0, 1.7.7, 1.5.5)
multi_json (1.7.7)
$ which ruby
...ruby-1.9.3-p448...

Finally, my code:

hash = YAML.load_file "my_yaml.yaml"
opts = {"indent"=>"\t", "space_before"=>" "}
json_pretty = JSON.pretty_generate(hash, opts)

The output, JSON.pretty_generate, does not seem to use the second argument, which should alter the output to the desired preferences.

like image 409
richie Avatar asked Jul 31 '26 14:07

richie


1 Answers

The keys to the options hash must be symbols.

opts =
{
   :indent => "\t",
   :space_before => " "
}
json_pretty = JSON.pretty_generate( obj, opts )
like image 165
Joey Hammer Avatar answered Aug 03 '26 04:08

Joey Hammer