Convert json to a useful ruby object
In a quest to find the absolute easiest way to create a useable object from a json doc, I read some of the ruby stdlib documentation and like so many issues I’ve come across, I quickly discovered that the briliant ruby community already had a simple solution for me.
Many 3rd party RESTful API’s deliver json as the response. The issue, is that I would like to take a json structure like the following and interact with it in a sane manner.
Before figuring out how to use the JSON parse options in Ruby, I thought that I would just have to navigate the array. So my old code looked like this.
I thought there must be a more natural rubyish way to handle the data instead of traversing a hash. Turns out that the JSON.parse command actually takes a number of options. One of those options is object_class. If you combine this object_class with OpenStruct, you get a really easy way to generate usable objects from raw json documents.
The final code looks like
The object_class can be any class, so it’s completely possible to create your own class that inherits from OpenStruct but has better methods for object serialization or access. A really nice combination would be to use ahoward’s map gem
That’s all for this post, hope you found it useful