# Does jason convert to hash?

**URL:** <https://www.ruby-forum.com/t/does-jason-convert-to-hash/193151>\
**Category:** Ruby\
**Created:** [August 12, 2010, 6:39am UTC](https://www.ruby-forum.com/t/does-jason-convert-to-hash/193151 "2010-08-12T06:39:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shortnose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5ac83/32.png) [@shortnose](https://www.ruby-forum.com/u/shortnose)\
**Post date:** [August 12, 2010, 6:39am UTC](https://www.ruby-forum.com/t/does-jason-convert-to-hash/193151/1 "2010-08-12T06:39:58Z")

</div>

I am trying to work with the Etsy API. I make a HTTP request and get  
data back in json format. I call JSON.parse(table) and the parse method  
returns the data in something that appears to be a hash.

Here is a simplified version of what JSON.parse(table) returns:

{“results”=\>[{“name”=\>“getMethodTable”, “uri”=\>“/”,  
“http\_method”=\>“GET”, “type”=\>“method”, “defaults”=\>nil, “params”=\>nil,  
“description”=\>“Get a list of all methods available.”,  
“visibility”=\>“public”}, {“name”=\>“getSubCategory”,  
“uri”=\>“/categories/:tag/:subtag”, “http\_method”=\>“GET”,  
“type”=\>“Category”, “defaults”=\>nil, “params”=\>{“subtag”=\>“string”,  
“tag”=\>“string”}, “description”=\>“Retrieves a second-level Category by  
tag and subtag.”, “visibility”=\>“public”}, {“name”=\>“getSubSubCategory”,  
“uri”=\>“/categories/:tag/:subtag/:subsubtag”, “http\_method”=\>“GET”,  
“type”=\>“Category”, “defaults”=\>nil, “params”=\>{“subtag”=\>“string”,  
“tag”=\>“string”, “subsubtag”=\>“string”}, “description”=\>“Retrieves a  
third-level Category by tag, subtag and subsubtag.”,  
“visibility”=\>“public”}], “type”=\>“method”, “count”=\>52, “params”=\>nil}

I am trying to access each of these hashes and each of the sub hashes. I  
do not think that I am using the correct syntax.

I can get some of the values by refering to:

table[“results”][i] # then iterate through i

This is the code I use to access the key value pairs.

require ‘rubygems’  
require ‘json’  
require ‘net/http’

class MethodTable # returns hash containing results from Etsy’s API  
method: getMethodTable  
def get\_table  
url = “[http://openapi.etsy.com/v2/sandbox/public/?api\_key=my\_key](http://openapi.etsy.com/v2/sandbox/public/?api_key=my_key)”  
url = URI.parse(url)  
method\_list = Net::HTTP.get(url)  
end  
end

method\_table = MethodTable.new  
table = method\_table.get\_table  
table = JSON.parse(table)

i = 1

while (i \<= table[“count”])

table[“results”][i].each do |key, value|  
print “#{key} =\> #{value}\n”  
end  
i += 1  
end

table.rb:21: undefined method `each’ for nil:NilClass (NoMethodError)

How can I use `each’ without and error? How can I access the sub-hashes  
such as the hash of parameters seen below?

“params”=\>{“subtag”=\>“string”, “tag”=\>“string”, “subsubtag”=\>“string”}

Thanks,

Sam

---

<div class="post-metadata">

**Author:** ![shortnose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5ac83/32.png) [@shortnose](https://www.ruby-forum.com/u/shortnose)\
**Post date:** [August 12, 2010, 7:04am UTC](https://www.ruby-forum.com/t/does-jason-convert-to-hash/193151/2 "2010-08-12T07:04:18Z")

</div>

in ur example data there are only 3 items in the “result” and the  
element of  
“count” has a value of 52  
in ur for-loop u will access 3-51 ones which do not exist  
u may use table[“results”].size instead of table[“count”]

2010/8/12 Samuel S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![shortnose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5ac83/32.png) [@shortnose](https://www.ruby-forum.com/u/shortnose)\
**Post date:** [August 12, 2010, 7:47am UTC](https://www.ruby-forum.com/t/does-jason-convert-to-hash/193151/3 "2010-08-12T07:47:01Z")

</div>

i wrote a test because ur code cannot be run on my machine

require ‘json’  
items = JSON.parse(‘{“test”:12334, “test1”: [{“a”:1,“b”:2},2,3]}’)  
items[“test1”][0].each{|item|  
p item  
}  
it looks good  
a hash can be “each”  
so try to “p” the variable before “each” it?

2010/8/12 Samuel S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![shortnose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5ac83/32.png) [@shortnose](https://www.ruby-forum.com/u/shortnose)\
**Post date:** [August 12, 2010, 7:24am UTC](https://www.ruby-forum.com/t/does-jason-convert-to-hash/193151/4 "2010-08-12T07:24:50Z")

</div>

jason joo wrote:

> in ur example data there are only 3 items in the “result” and the  
> element of  
> “count” has a value of 52  
> in ur for-loop u will access 3-51 ones which do not exist  
> u may use table[“results”].size instead of table[“count”]
> 
> 2010/8/12 Samuel S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

In my while loop I am using "table[“count”]. If you run this code it  
produces output that starts with:

"{"count":52,"results":[{"name":"getMethodTable","description":"Get  
a list of all methods  
available.","uri":"\/","params":null,"defaults":null,"type":"method","visibility":"public","http\_method":"GET"}

My while loop is using table[“count”] as the limit. Sorry for the  
confusion.
