Parsing JSON -> Ruby in Rails?

Charlie Hubbard 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