# Httparty parsed response

**URL:** <https://www.ruby-forum.com/t/httparty-parsed-response/214714>\
**Category:** Ruby\
**Created:** [December 9, 2011, 9:16pm UTC](https://www.ruby-forum.com/t/httparty-parsed-response/214714 "2011-12-09T21:16:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [December 9, 2011, 9:16pm UTC](https://www.ruby-forum.com/t/httparty-parsed-response/214714/1 "2011-12-09T21:16:01Z")

</div>

Hi all,

I am using httparty to get the content of a service.

@reponse = httparty.get(url)

puts @reponse.parsed\_response

parsed\_reponse is as follows,

@parsed\_response =\>{“name” =\> “ruby”, “number” =\> {“item”=\>{"@no"=\>“1”,  
“itemcontent”=\>{“itemcategory”=\>{"@itemtype"=\>“Book”,  
“@itemtag”=\>“Generak”}}, “form”=\>“text”, “shape”=\>“square”,  
“dimension”=\>“full”, “type”=\>{“category”=\>{"@itemid"=\>“24”,  
“@itemexpiry”=\>“false”, “@quote”=\>"[FRESH]", “$”=\>“2010”}},  
“expiryComments”=\>{“expiryStatus”=\>{"@expiry"=\>“true”, “$”=\>“2012”}}},  
“shop”=\>{“shopName”=\>“STORE”}}}

I need to parse each and every hash and its elements and even array of  
elements inside each hash

for example say,

I need to parse and equate,

name = “rub”

@parsed\_response[“name”].should == name

this will result  
expected: rub  
got: ruby

similarly, for each and every hash I need to check the value with some  
other value, whether they are equal or not.

Can anyone kindly explain with code, how to parse each value of the  
above response.

the above parsed response is only one such record, but i do have  
multiple records to check like

@parsed\_response =\> {“name” =\> “ruby”, “number” =\> {“item”=\>{"@no"=\>“1”,  
…}},{“item” =\> {"@no" = \>“2”, …}},{“item” =\> {"@no" = \>“3”,  
…}}}

In this case I need to check for all the values. need assistance for  
this.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![rubythemystery](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@rubythemystery](https://www.ruby-forum.com/u/rubythemystery)\
**Post date:** [December 11, 2011, 8:03pm UTC](https://www.ruby-forum.com/t/httparty-parsed-response/214714/2 "2011-12-11T20:03:53Z")

</div>

Can Any one Help me on this

---

<div class="post-metadata">

**Author:** ![rubythemystery](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@rubythemystery](https://www.ruby-forum.com/u/rubythemystery)\
**Post date:** [January 14, 2013, 8:13pm UTC](https://www.ruby-forum.com/t/httparty-parsed-response/214714/3 "2013-01-14T20:13:31Z")

</div>

rubythemystery ruby wrote in post #1036178:

> Can Any one Help me on this

The response is in standard JSON format. Look into the JSON gem (IIRC,  
included in most default ruby installs). The method you’ll want  
specifically is “JSON.parse @reponse.parsed\_response”. That will return  
the JSON data in native ruby format as hashes, arrays, strings, etc.  
From there, you can manipulate the data using those objects’ methods.

---

<div class="post-metadata">

**Author:** ![rubythemystery](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@rubythemystery](https://www.ruby-forum.com/u/rubythemystery)\
**Post date:** [January 12, 2013, 12:34am UTC](https://www.ruby-forum.com/t/httparty-parsed-response/214714/4 "2013-01-12T00:34:21Z")

</div>

rubythemystery ruby wrote in post #1036005:

> Hi all,
> 
> I am using httparty to get the content of a service.
> 
> @reponse = httparty.get(url)
> 
> puts @reponse.parsed\_response
> 
> parsed\_reponse is as follows,
> 
> @parsed\_response =\>{“name” =\> “ruby”, “number” =\> {“item”=\>{“@no”=\>“1”,  
> “itemcontent”=\>{“itemcategory”=\>{“@itemtype”=\>“Book”,  
> “@itemtag”=\>“Generak”}}, “form”=\>“text”, “shape”=\>“square”,  
> “dimension”=\>“full”, “type”=\>{“category”=\>{“@itemid”=\>“24”,  
> “@itemexpiry”=\>“false”, “@quote”=\>“[FRESH]”, “$”=\>“2010”}},  
> “expiryComments”=\>{“expiryStatus”=\>{“@expiry”=\>“true”, “$”=\>“2012”}}},  
> “shop”=\>{“shopName”=\>“STORE”}}}
> 
> I need to parse each and every hash and its elements and even array of  
> elements inside each hash
> 
> for example say,
> 
> I need to parse and equate,
> 
> name = “rub”
> 
> @parsed\_response[“name”].should == name
> 
> this will result  
> expected: rub  
> got: ruby
> 
> similarly, for each and every hash I need to check the value with some  
> other value, whether they are equal or not.
> 
> Can anyone kindly explain with code, how to parse each value of the  
> above response.
> 
> the above parsed response is only one such record, but i do have  
> multiple records to check like
> 
> @parsed\_response =\> {“name” =\> “ruby”, “number” =\> {“item”=\>{“@no”=\>“1”,  
> …}},{“item” =\> {“@no” = \>“2”, …}},{“item” =\> {“@no” = \>“3”,  
> …}}}
> 
> In this case I need to check for all the values. need assistance for  
> this.
> 
> Thanks in advance

I’m still a bit of a nube with ruby but I have been working with this  
library a little bit.  
Using your example response:

def check\_and\_change  
res=@parsed\_response  
name=‘rub’  
itemtype=‘Book’

# if the name in your results does not equal ‘rub’ changes the value to

‘rub’  
if res[‘name’] != name  
res[‘name’]=name  
end

#if the item type is ‘Book’ puts ‘Win’

if res[‘number’][‘item’][‘itemcontent’][‘itemcatagory’][‘@itemtype’]==  
itemtype  
puts ‘Win’  
end

#If there are multiple ‘item’ records  
assuming it looks like this:

> @parsed\_response =\> {“name” =\> “ruby”, “number” =\> {“item”=\>{“@no”=\>“1”,  
> …}},{“item” =\> {“@no” = \>“2”, …}},{“item” =\> {“@no” = \>“3”,  
> …}}}

if  
res[‘number’].first[‘item’][‘itemcontent’][‘itemcatagory’][‘@itemtype’]==  
itemtype  
puts ‘first Win’  
end  
if  
res[‘number’].last[‘item’][‘itemcontent’][‘itemcatagory’][‘@itemtype’]==  
itemtype  
puts ‘last Win’  
end

Hope that makes sense and helps
