Convert Complex JSON Response to my own Object

So I use HTTParty to interact with an API for an internal web application. The API is written in C#. I was able to take a look at the code that maps to the JSON API response and looking to create the class structure that maps to the API response just like in the C# code. I have all of the classes created but how can I map the JSON response to the class I created? The JSON is complex with anything from nested hashes, arrays, arrays of objects containing nested hashes, etc. I know in other languages like C# and Java it’s a few lines (if not one line). How can I do it in Ruby? I have all of the properties/attributes declared as attr_accessors with a few classes having some methods on them as well just like in the C# code.
Incase anyone is wondering, the reason for this mapping is because I’m writing some workflow functional tests for the application and I need the data to be in a certain status before I can operate on it. It can be done through the UI but that would add additional time to the test and the workflow operations required are actually a separate test itself. So I wanted to add the required data by setting values in the mapped class and then make a post request with the object. I think it would be easier to work with a typed object versus a generic hash type that HTTParty returns.

Hey @Bmoe,

Is that what you are looking for: Convert json to a useful ruby object?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs