Wednesday, 28 August 2013

In Ruby, how to output json from hash and give it line breaks and tabs

In Ruby, how to output json from hash and give it line breaks and tabs

I'm trying to format {"key" => "value"} to turn it into :
{
"key" : "value"
}
for writing into a json file. right now im doing :
hash = {"key" => "value"}
puts hash.to_json.gsub('{', '{\n\t')
to start. and this outputs
{\n\t"key":"value"}
Why cant i do the line break?

No comments:

Post a Comment