Ruby JSON.pretty_generate ... is pretty unpretty

28,710

Solution 1

To generate pretty JSON output it appears that you're only missing a puts call.

The data:

some_data = {'foo' => 1, 'bar' => 20, 'cow' => [1, 2, 3, 4], 'moo' => {'dog' => 'woof', 'cat' => 'meow'}}

Calling only JSON.pretty_generate:

> JSON.pretty_generate some_data
 => "{\n  \"foo\": 1,\n  \"bar\": 20,\n  \"cow\": [\n    1,\n    2,\n    3,\n    4\n  ],\n  \"moo\": {\n    \"dog\": \"woof\",\n    \"cat\": \"meow\"\n  }\n}"

Adding a puts into the mix:

> puts JSON.pretty_generate some_data
{
  "foo": 1,
  "bar": 20,
  "cow": [
    1,
    2,
    3,
    4
  ],
  "moo": {
    "dog": "woof",
    "cat": "meow"
  }
}

Solution 2

I use Rails 2.3.8 and installed the JSON gem (gem install json). JSON.pretty_generate now does nicely in script/console:

>> some_data = {'foo' => 1, 'bar' => 20, 'cow' => [1, 2, 3, 4], 'moo' => {'dog' => 'woof', 'cat' => 'meow'}}
=> {"cow"=>[1, 2, 3, 4], "moo"=>{"cat"=>"meow", "dog"=>"woof"}, "foo"=>1, "bar"=>20}
>> JSON.pretty_generate(some_data)
=> "{\n  \"cow\": [\n    1,\n    2,\n    3,\n    4\n  ],\n  \"moo\": {\n    \"cat\": \"meow\",\n    \"dog\": \"woof\"\n  },\n  \"foo\": 1,\n  \"bar\": 20\n}"
Share:
28,710
Amy
Author by

Amy

Assists with the design and implementation of web projects.

Updated on July 09, 2022

Comments

  • Amy
    Amy almost 2 years

    I can't seem to get JSON.pretty_generate() to actually generate pretty output in Rails.

    I'm using Rails 2.3.5 and it seems to automatically load the JSON gem. Awesome. While using script/console this does indeed produce JSON:

    some_data = {'foo' => 1, 'bar' => 20, 'cow' => [1, 2, 3, 4], 'moo' => {'dog' => 'woof', 'cat' => 'meow'}}
    some_data.to_json
    => "{\"cow\":[1,2,3,4],\"moo\":{\"cat\":\"meow\",\"dog\":\"woof\"},\"foo\":1,\"bar\":20}"
    

    But this doesn't produce pretty output:

    JSON.pretty_generate(some_data)
    => "{\"cow\":[1,2,3,4],\"moo\":{\"cat\":\"meow\",\"dog\":\"woof\"},\"foo\":1,\"bar\":20}"
    

    The only way I've found to generate it is to use irb and to load the "Pure" version:

    require 'rubygems'
    require 'json/pure'
    some_data = {'foo' => 1, 'bar' => 20, 'cow' => [1, 2, 3, 4], 'moo' => {'dog' => 'woof', 'cat' => 'meow'}}
    JSON.pretty_generate(some_data)
    => "{\n  \"cow\": [\n    1,\n    2,\n    3,\n    4\n  ],\n  \"moo\": {\n    \"cat\": \"meow\",\n    \"dog\": \"woof\"\n  },\n  \"foo\": 1,\n  \"bar\": 20\n}"
    

    BUT, what I really want is Rails to produce this. Does anyone have any tips why I can't get the generator in Rails to work correctly?

    Thanks!

  • Amy
    Amy almost 14 years
    Go figure. It works now. I also recently upgraded to 2.3.8.
  • Nowaker
    Nowaker over 10 years
    The output that OP provided doesn't contain any \ns, so puts is not going to change anything.
  • Geoff Petrie
    Geoff Petrie over 10 years
    @DamianNowak I'm not sure if I understand what your objection is. In my example I'm using the same input as the OP specified and then demonstrating pretty output through the use of puts in irb. Could you please clarify what you mean?
  • Nowaker
    Nowaker over 10 years
    pretty_generate doesn't work for OP. There are no \n or indent spaces in OP's output, as you can see. So puts is not going to change anything.
  • Konstantin
    Konstantin over 8 years
    @Geoff Petrie You can see the \n in first part of your code that are absent in OP's output. That is why the puts will not change anything for OP.
  • Konstantin
    Konstantin over 8 years
    @Amy, doesn't it mean this answer should be the accepted one?