1

I've run into a situation where I'm retrieving a json string in ruby, but I'm attempting to write it to a file structured like your usual json file (for example:

{
    "top":{
        "mid1":"bot1", 
        "mid2":"bot2"
    }
}

However, the way the json string is structured is like so:

{"top":{"mid1":"bot1","mid2":"bot2"}}

I've seen other posts mention doing JSON.parse in order to get what I want, however that just causes it to look like this:

{"top"=>{"mid1"=>"bot1", "mid2"=>"bot2"}}

Is there a way that I can just convert the json string into what it looks like in the first code block above?

  • The two English words most commonly used in the industry for this are "pretty" and "tidy". Using those words should help you search for solutions. Good luck! – Jared Beck Feb 18 '20 at 23:18

1 Answers1

1

The first representation is JSON. The decoded version is Ruby's internal representation. If you want to go back to JSON, JSON.dump(...) will reconvert it.

Keep in mind this is just presentation. The data is the same.

You may be interested in the pp method as that produces nice, structured debugging output.

tadman
  • 208,517
  • 23
  • 234
  • 262