Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6139013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:56:12+00:00 2026-05-23T17:56:12+00:00

Using ActiveRecord::Base.to_json I do: user = User.find_by_name ‘Mika’ {created_at:2011-07-10T11:30:49+03:00,id:5,is_deleted:null,name:Mika} Now, what I would like

  • 0

Using ActiveRecord::Base.to_json I do:

user = User.find_by_name 'Mika'

{"created_at":"2011-07-10T11:30:49+03:00","id":5,"is_deleted":null,"name":"Mika"}

Now, what I would like to have is:

{
    "created_at":"2011-07-10T11:30:49+03:00",
    "id":5,
    "is_deleted":null,
    "name":"Mika"
}

Is there an option to do this?

It would be great to have a global option, so that the behaviour be set depending on dev/live environment.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:56:13+00:00Added an answer on May 23, 2026 at 5:56 pm

    I’ll go out on a limb and say “no, there is no such option”.

    AFAIK, the JSON encoding is actually handled by ActiveSupport rather than ActiveRecord. If you look at lib/active_support/json/encoding.rb for your ActiveSupport gem, you’ll see a lot of monkey patching going on to add as_json and encode_json methods to some core classes; the as_json methods are just used to flatten things like Time, Regexp, etc. to simpler types such as String. The interesting monkey patches are the encode_json ones, those methods are doing the real work of producing JSON and there’s nothing in them for controlling the final output format; the Hash version, for example, is just this:

    # comments removed for clarity
    def encode_json(encoder)
      "{#{map { |k,v| "#{encoder.encode(k.to_s)}:#{encoder.encode(v, false)}" } * ','}}"
    end
    

    The encoder just handles things like Unicode and quote escaping. As you can see, the encode_json is just mashing it all together in one compact string with no options for enabling prettiness.

    All the complex classes appear to boil down to Hash or Array during JSONification so you could, in theory, add your own monkey patches to Hash and Array to make them produce something pretty. However, you might have some trouble keeping track of how deep in the structure you are so producing this:

    {
        "created_at":"2011-07-10T11:30:49+03:00",
        "id":5,
        "is_deleted":null,
        "name":"Mika"
        "nested":{
        "not":"so pretty now",
        "is":"it"
    }
    }
    

    Would be pretty straight forward but this:

    {
        "created_at":"2011-07-10T11:30:49+03:00",
        "id":5,
        "is_deleted":null,
        "name":"Mika"
        "nested": {
            "not":"so pretty now",
            "is":"it"
        }
    }
    

    would be harder and, presumably, you’d want the latter and especially so with deeply nested JSON where eyeballing the structure is difficult. You might be able to hang a bit of state on the encoder that gets passed around but that would be getting a little ugly and brittle.

    A more feasible option would be an output filter to parse and reformat the JSON before sending it off to the browser. You’d have to borrow or build the pretty printer but that shouldn’t be that difficult. You should be able to conditionally attach said filter only for your development environment without too much ugliness as well.

    If you’re just looking to debug your JSON based interactions then maybe the JSONovich extension for Firefox would be less hassle. JSONovich has a few nice features (such as expanding and collapsing nested structures) that go beyond simple pretty printing too.


    BTW, I reviewed Rails 3.0 and 3.1 for this, you can check Rails 2 if you’re interested.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Rails 2.3.5. Class User < ActiveRecord::Base has_many :phones end class Phone
I'm using Rails 3 w/ Mongoid, (so no ActiveRecord). Mongoid uses ActiveModel's to_json method,
I have the following associations: class User < ActiveRecord::Base has_and_belongs_to_many :brands, :join_table => 'brands_users'
class Office < ActiveRecord::Base has_many :users searchable do text :name location :coordinates do Sunspot::Util::Coordinates.new(latitude,
I'm using 2 joined models class Product < ActiveRecord::Base has_and_belongs_to_many :providers end class Provider
I have two models, User and TrainingSession: class User < ActiveRecord::Base has_many :training_sessions end
I want to execute a very long query in Rails using ActiveRecord::Base.connection.execute(sql) . However,
I have two models class User < ActiveRecord::Base has_one :user_information, :dependent => :destroy attr_accessible
I am using formtastic like this class Court < ActiveRecord::Base belongs_to :tournament end class
I am using Rails 3 and here is my model class LineItem < ActiveRecord::Base

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.