# Parsing JSON -\> Ruby in Rails?

**URL:** <https://www.ruby-forum.com/t/parsing-json-ruby-in-rails/84477>\
**Category:** Rails\
**Created:** [February 5, 2007, 5:11am UTC](https://www.ruby-forum.com/t/parsing-json-ruby-in-rails/84477 "2007-02-05T05:11:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chubbard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e56c9b/32.png) [@chubbard](https://www.ruby-forum.com/u/chubbard)\
**Post date:** [February 5, 2007, 5:11am UTC](https://www.ruby-forum.com/t/parsing-json-ruby-in-rails/84477/1 "2007-02-05T05:11:47Z")

</div>

Hi,

I know that Rails has to\_json method in 1.1, but what do I use to  
convert json -\> ruby objects in my controllers? I’ve tried the JSON  
ruby parser, but when I include that in my controller it overwrites  
rail’s to\_json method with it’s own that doesn’t work right. When I  
don’t include json library the following renders:

render( :text =\> output.to\_json )

“{“attributes”: {“text”: “Redirection is done using either the  
\342\200\234\>\342\200\235 (greater-than symbol), or using the  
\342\200\234|\342\200\235 (pipe) operator which sends the standard  
output of one command to another command as standard input.”,  
“paragraph”: “7”, “page\_id”: “36”, “id”: “1”, “user\_id”:  
“1”}}”

This is strange to include the attributes array by default, but it  
works. When I include the library I get:

“”#Highlight:0x365b410""

If I’m not supposed to use json library, then what does rails provide  
out of the box for going from json -\> ruby?

Charlie

---

<div class="post-metadata">

**Author:** ![chubbard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e56c9b/32.png) [@chubbard](https://www.ruby-forum.com/u/chubbard)\
**Post date:** [February 5, 2007, 10:33am UTC](https://www.ruby-forum.com/t/parsing-json-ruby-in-rails/84477/2 "2007-02-05T10:33:06Z")

</div>

Charlie H. wrote:

> If I’m not supposed to use json library, then what does rails provide  
> out of the box for going from json → ruby?

I can’t find anything currently built-in, but I’ve been successfully  
using SafeJSON.rb (put it in the lib directory):

[http://www.xerial.org/projects/Xerial/browser/ruby/trunk/aqua/mixi/SafeJSON.rb?format=txt](http://www.xerial.org/projects/Xerial/browser/ruby/trunk/aqua/mixi/SafeJSON.rb?format=txt)

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![chubbard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e56c9b/32.png) [@chubbard](https://www.ruby-forum.com/u/chubbard)\
**Post date:** [February 5, 2007, 10:57am UTC](https://www.ruby-forum.com/t/parsing-json-ruby-in-rails/84477/3 "2007-02-05T10:57:09Z")

</div>

Charlie H. wrote:

> I know that Rails has to\_json method in 1.1, but what do I use to  
> convert json → ruby objects in my controllers?

Except for some whitespace all JSON content can be loaded using the  
native  
ruby YAML parser. If your JSON outputer is not YAML safe (needs to  
insert  
whitespace after colons) use the following snippet:

> YAML.load(json\_str.gsub(/,“(\w+)”/, ‘, “\1”’).gsub(/“(\w+)”:/, '“\1”: '))

Hopefully in the future this extra step will no longer be needed and  
JSON  
will be a true subset of YAML.

zsombor

–  
Company - [http://primalgrasp.com](http://primalgrasp.com)  
Thoughts - [http://deezsombor.blogspot.com](http://deezsombor.blogspot.com)
