How to pretty format JSON output in Ruby on Rails

0 votes
I would like my JSON output in Ruby on Rails to be "pretty" or nicely formatted.

Right now, I call to_json and my JSON is all on one line. At times this can be difficult to see if there is a problem in the JSON output stream.

Is there way to configure to make my JSON "pretty" or nicely formatted in Rails?
Jul 24, 2020 in Java-Script by kartik
• 37,520 points
11,774 views

1 answer to this question.

0 votes

Hello @kartik,

Use the pretty_generate() function, built into later versions of JSON. For example:

require 'json'
my_object = { :array => [1, 2, 3, { :sample => "hash"} ], :foo => "bar" }
puts JSON.pretty_generate(my_object)

Which gets you:

{
  "array": [
    1,
    2,
    3,
    {
      "sample": "hash"
    }
  ],
  "foo": "bar"
}

Hope it helps!!

Thank You!!

answered Jul 24, 2020 by Niroj
• 82,840 points

Related Questions In Java-Script

0 votes
1 answer

How do I parse JSON with Ruby on Rails?

Hello @kartik, Use  hash = JSON.parse string Rails should automagically ...READ MORE

answered Jul 25, 2020 in Java-Script by Niroj
• 82,840 points
4,442 views
0 votes
1 answer

How do I turn a string to a json in Node.js?

Hello Kartik, Use the JSON function  JSON.parse(theString) ...READ MORE

answered Apr 24, 2020 in Java-Script by Niroj
• 82,840 points
973 views
0 votes
1 answer

How to convert a ruby hash object to JSON?

Hello @kartik, You can also use JSON.generate: require 'json' JSON.generate({ foo: ...READ MORE

answered Jul 24, 2020 in Java-Script by Niroj
• 82,840 points
12,666 views
0 votes
2 answers

How to animate a change in backgroundColor using jQuery on mouseover?

You don't need jquery. You can also ...READ MORE

answered Sep 9, 2020 in Java-Script by Eureka
• 160 points
1,996 views
0 votes
1 answer

How to rollback a specific migration?

Hello @kartik, Try this: rake db:migrate:down VERSION=20100905201547 will roll back ...READ MORE

answered Jul 24, 2020 in Java-Script by Niroj
• 82,840 points
3,114 views
0 votes
1 answer

How to check if a specific key is present in a hash or not?

Hello @kartik, While Hash#has_key? gets the job done, as it has ...READ MORE

answered Jul 24, 2020 in Java-Script by Niroj
• 82,840 points
3,322 views
0 votes
1 answer

How to drop columns using Rails migration?

Hello @kartik, Use this: remove_column :table_name, :column_name For instance: remove_column :users, ...READ MORE

answered Jul 24, 2020 in Java-Script by Niroj
• 82,840 points
1,068 views
0 votes
1 answer

How can I specify a local gem in my Gemfile?

Hello @kartik, In order to use local gem ...READ MORE

answered Jul 24, 2020 in Java-Script by Niroj
• 82,840 points
3,685 views
0 votes
1 answer

How to print a circular structure in a JSON-like format?

Hello, You can use util.inspect(object) in node.js. It automatically ...READ MORE

answered Apr 24, 2020 in Java-Script by Niroj
• 82,840 points
2,763 views
0 votes
1 answer

How can I rename a database column in a Ruby on Rails migration?

Hello @kartik, Try using this: rename_column :table, :old_column, :new_column You'll ...READ MORE

answered Jul 24, 2020 in Java-Script by Niroj
• 82,840 points
1,588 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP