It puts a ruby object such as a hash or an array in the argument and returns it in JSON format.
JSON.generate
pry(main)> JSON.generate({"hoge"=>{"fuga"=>1, "fugafuga"=>2}})
=> "{\"hoge\":{\"fuga\":1,\"fugafuga\":2}}"
JSON.pretty_generate
pry(main)> JSON.pretty_generate({"hoge"=>{"fuga"=>1, "fugafuga"=>2}})
=> "{\n \"hoge\": {\n \"fuga\": 1,\n \"fugafuga\": 2\n }\n}"
It returns a string that is easier to read than JSON.generate.
It was said that it is better to use generate because generate is lighter in capacity unless it is read by humans.
Recommended Posts