Have XML, want JSON

I’m getting an XML-document form a third party URL, parse it using
Hpricot and Xpath and wish to return the result as JSON. However, I
can’t find a decent way to convert XML to JSON, is there a nice Railsy
shortcut for this that I haven’t discovered?

Thanks!

There is in fact a wonderful little thing known as Object#to_json
check it out: Peak Obsession

  • Kyle

Kyle Neath wrote:

There is in fact a wonderful little thing known as Object#to_json
check it out: Peak Obsession

  • Kyle

Thanks, but I’ve already tried it and it doesn’t work (get
ActiveSupport::JSON::CircularReferenceError).

My guess is that I need to convert the XML to a hash, which I’ve tried
using Hash.from_xml(parsed_xml) but I get a NameError in the controller:
“undefined local variable or method `source’ for
REXML::SourceFactory:Class”.

Anyone know of another way besides Hash.from_xml to convert the XML to a
hash before I convert it to JSON?

Just a guess, but you might want to investigate the options available
for :to_json. For example, passing :only=>[attr_list] for an array
will cause it to render only the named attributes for each member of
the collection. It’s possible that by the right combination of
options you can break the circular reference issue.