Phil Phil - 1 year ago 98
Ruby Question

Handling better response from

First time trying to use the Ruby AWS ADK V2 and I am trying to format the data i am getting back and it seems quiet hard getting it into useable format.

All I want to do is get a list of Hosted Zones and display in a table.

I have a helper that has:

def hosted_zones
r53 =
#convert to hash first so we can parse and covert to json
h = (r53.list_hosted_zones).to_hash
j = JSON.parse((h.to_json))

which then returns me the following JSON:

"hosted_zones": [{
"id": "/hostedzone/Z1HSDGASSSME",
"name": "",
"caller_reference": "2016-07-12T15:33:45.277646707+01:00",
"config": {
"comment": "Private DNS zone for stage",
"private_zone": true
"resource_record_set_count": 10
}, {
"id": "/hostedzone/ZJDGASSS0ZN3",
"name": "",
"caller_reference": "2016-07-12T15:33:41.290143511+01:00",
"config": {
"comment": "Public DNS zone for stage",
"private_zone": false
"resource_record_set_count": 7
"is_truncated": false,
"max_items": 100

To which I am running a really but while statement to interact through all the hosted_zone entries into a table.

Is this the best way to get the response or can you request the response to be json already?

Answer Source

Why are you converting a hash to JSON, only to convert it to a hash again? JSON.parse(some_hash.to_json) will just give you some_hash.

That being said, I don't think it is possible to get JSON directly from AWS, mainly due to the fact that their API responds with XML. I think that your solution is ideal if that's all you're doing, but if you want, you can make a request with an HTTP client and then take the XML that you receive and use something like ActiveSupport's Hash.from_xml to create a hash that you can then convert to JSON.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download