# Access values for JSON.parse response

**URL:** <https://www.ruby-forum.com/t/access-values-for-json-parse-response/227331>\
**Category:** Ruby\
**Created:** [March 11, 2013, 9:35pm UTC](https://www.ruby-forum.com/t/access-values-for-json-parse-response/227331 "2013-03-11T21:35:34Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![nvillette](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nvillette](https://www.ruby-forum.com/u/nvillette)\
**Post date:** [March 11, 2013, 9:35pm UTC](https://www.ruby-forum.com/t/access-values-for-json-parse-response/227331/1 "2013-03-11T21:35:34Z")

</div>

Hello, Does anyone know who to get the values from a nested hash in  
Ruby.  
I made the following request which after doing the JSON.parse, it  
returns a nested hash with arrays. So, I am trying to figure out how to  
access the values in the response. See below for response.

uri =  
URI.parse(‘[http://nvillette.dev.targetspot.com/tod.php?station=KRAYFM&length=30&zip=28902](http://nvillette.dev.targetspot.com/tod.php?station=KRAYFM&length=30&zip=28902)’)

res = Net::HTTP.get\_response(uri)

result= res.body

puts parsed = JSON.parse(result)

Ex. How would I verify that the duration is 30?

Response:

{“TOD”:{“version”:“1.0”,“AdBreaks”:{“AdBreak”:[{“adType”:“AUDIO”,“duration”:30,“adCount”:1,“Ad”:[{“id”:611,“acc”:225,“lastModified”:“2013-03-11T19:22:15”,“order”:1,“adType”:“AUDIO”,“duration”:30,“MediaFile”:{“uri”:“http://content.dev.targetspot.com/content/act/225/adunit/86/02/1363029493701178\_225.mp3”,“type”:“audio/mpeg”,“bitrate”:64},“Impression”:{“Log”:[{“uri”:“http://nvillette.dev.targetspot.com/log.php?data=VVNFUj00YzRhN2FjZi1jMWIzLTQxMWItOTAzMy1kMTZiMzI0YTM3YzMmU1RBVD1LUkFZRk0mSUQ9NjExJkFDQ09VTlRJRD0yMjUmQURVTklUSUQ9NTEyJlBSPTI1Jk1QUj0yNSZDVD1TMzAmUkVQPTkwMCZaPTI4OTAyJk1BUktFVElEPTUyNCZTVEFURT1OQyZHRU9QUkVGSUQ9MSZSRUdJT05JRD04JkNBQ0hFQlVTVEVSPTEzNjMwMzM2ODcmQk49MyZWPVRPRCZHPTAmQT0xJkk9MCZWSVNVQUxNT0RFPTImRkxBU0hFTkFCTEVEPTAmVEhJUkRQQVJUWVNUQVRVUz0xJlRTQU5BTFlUSUNTPTEmTVNBPTAmQURTT1VSQ0VTRUxFQ1RFRD0xJkFEU09VUkNFQVRURU1QVEVEJTVCMCU1RD0xJk1FVEhPRD0yJk1PQklMRT0wJlBSRVJPTEw9MCZDTEs9MCZET1dOTE9BRD0wJkNBTExDTElDSz0wJlJBVEVMT0c9MCZJR05PUkVfR0VPX0lQPTAmVFo9QW1lcmljYSUyRk5ld19Zb3JrJkNUQ0Y9LTEmU1RDRj0tMSZDSUNGPS0xJkdNPVpJUCZEUz0wLjAwMDAwMDAwMDAmQURCUkVBSz0zMA==”,“event”:“complete”,“source”:“TargetSpot”}]},“CompanionVisual”:{“MediaFile”:[{“uri”:“http://content.dev.targetspot.com/content/act/225/visual/89/9f/180/135/1363029483307708\_225.jpg”,“type”:“jpg”,“width”:180,“height”:135},{“uri”:“http://content.dev.targetspot.com/content/act/225/visual/89/9f/180/150/1363029483307708\_225.jpg”,“type”:“jpg”,“width”:180,“height”:150},{“uri”:“http://content.dev.targetspot.com/content/act/225/visual/89/9f/300/250/1363029483307708\_225.jpg”,“type”:“jpg”,“width”:300,“height”:250},{“uri”:“http://content.dev.targetspot.com/content/act/225/visual/89/9f/320/240/1363029483307708\_225.jpg”,“type”:“jpg”,“width”:320,“height”:240},{“uri”:“http://content.dev.targetspot.com/content/act/225/visual/89/9f/336/280/1363029483307708\_225.jpg”,“type”:“jpg”,“width”:336,“height”:280}]}}]}]}}}

---

<div class="post-metadata">

**Author:** ![nvillette](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nvillette](https://www.ruby-forum.com/u/nvillette)\
**Post date:** [March 12, 2013, 4:30am UTC](https://www.ruby-forum.com/t/access-values-for-json-parse-response/227331/2 "2013-03-12T04:30:21Z")

</div>

On Mar 11, 2013 4:16 PM, “Matthew K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> parsed[‘TOD’][‘AdBreaks’][‘AdBreak’][0][‘duration’]
> 
> # or
> 
> parsed[‘TOD’][‘AdBreaks’][‘AdBreak’].select{|ad|  
> ad[‘duration’] == 30 }
> 
> Also note: this is yucky. Yucky yucky yucky. A saner person might use  
> some local variables. Even one will help a lot:
> 
> adbreaks = parsed[‘TOD’][‘AdBreaks’][‘AdBreak’]
> 
> # while I’m here: why does ‘AdBreaks’ hold one item,
> 
> # but ‘AdBreak’ hold an array? Odd.

These are the vagaries of XML 🙂

---

<div class="post-metadata">

**Author:** ![nvillette](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nvillette](https://www.ruby-forum.com/u/nvillette)\
**Post date:** [March 11, 2013, 10:14pm UTC](https://www.ruby-forum.com/t/access-values-for-json-parse-response/227331/3 "2013-03-11T22:14:55Z")

</div>

Nicole V. wrote in post #1101133:

> Hello, Does anyone know [how] to get the values from a nested hash in  
> Ruby.

Yep.

> I made the following request which after doing the JSON.parse, it  
> returns a nested hash with arrays. So, I am trying to figure out how to  
> access the values in the response. See below for response.

Since your “parsed” object is a Hash, you can access the first level of  
nesting with:  
parsed[‘TOD’]  
which returns a Hash, so you can access the next level with:  
parsed[‘TOD’][‘AdBreaks’]  
and so on.

At some point in your structure there’s an Array instead of a Hash, so  
you’d need to use something like:

parsed[‘TOD’][‘AdBreaks’][‘AdBreak’][0][‘duration’]

# or

parsed[‘TOD’][‘AdBreaks’][‘AdBreak’].first[‘duration’]

But personally I’d probably go for one of the following:

parsed[‘TOD’][‘AdBreaks’][‘AdBreak’].map{|ad|  
ad[‘duration’] }

# or

parsed[‘TOD’][‘AdBreaks’][‘AdBreak’].any?{|ad|  
ad[‘duration’] == 30 }

# or

parsed[‘TOD’][‘AdBreaks’][‘AdBreak’].select{|ad|  
ad[‘duration’] == 30 }

Also note: this is yucky. Yucky yucky yucky. A saner person might use  
some local variables. Even one will help a lot:

adbreaks = parsed[‘TOD’][‘AdBreaks’][‘AdBreak’]

# while I’m here: why does ‘AdBreaks’ hold one item,

# but ‘AdBreak’ hold an array? Odd.

lengths\_of\_breaks = adbreaks.map {|ad| ad[‘duration’] }  
list\_of\_30s\_breaks = adbreaks.select {|ad| ad[‘duration’] == 30 }

# etc.

---

<div class="post-metadata">

**Author:** ![nvillette](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nvillette](https://www.ruby-forum.com/u/nvillette)\
**Post date:** [March 12, 2013, 6:24pm UTC](https://www.ruby-forum.com/t/access-values-for-json-parse-response/227331/4 "2013-03-12T18:24:03Z")

</div>

Any reason you don’t just write a recursive method? I find that’s the  
easiest way to extract values from nested structures.

---

<div class="post-metadata">

**Author:** ![nvillette](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nvillette](https://www.ruby-forum.com/u/nvillette)\
**Post date:** [March 12, 2013, 5:41pm UTC](https://www.ruby-forum.com/t/access-values-for-json-parse-response/227331/5 "2013-03-12T17:41:02Z")

</div>

tamouse mailing lists wrote in post #1101156:

> On Mar 11, 2013 4:16 PM, “Matthew K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > parsed[‘TOD’][‘AdBreaks’][‘AdBreak’][0][‘duration’]
> > 
> > # or
> > 
> > parsed[‘TOD’][‘AdBreaks’][‘AdBreak’].select{|ad|  
> > ad[‘duration’] == 30 }
> > 
> > Also note: this is yucky. Yucky yucky yucky. A saner person might use  
> > some local variables. Even one will help a lot:
> > 
> > adbreaks = parsed[‘TOD’][‘AdBreaks’][‘AdBreak’]
> > 
> > # while I’m here: why does ‘AdBreaks’ hold one item,
> > 
> > # but ‘AdBreak’ hold an array? Odd.
> 
> These are the vagaries of XML 🙂

I have another question. By looking at my response, I am trying to go  
deeper to access this value: “Impression”:{“Log”:[{“uri”

I am attempting to do the following:

uri=  
parsed[‘TOD’][‘AdBreaks’][‘AdBreak’][0][‘Ad’][0][‘MediaFile’][‘Impression’][‘Log’][0][‘uri’]

but it does not work. I get the following: NoMethodError: undefined  
method `’ for nil:NilClass

This seems to work fine, which is a level above:

bitrate=parsed[‘TOD’][‘AdBreaks’][‘AdBreak’][0][‘Ad’][0][‘MediaFile’][‘bitrate’]

Thanks

---

<div class="post-metadata">

**Author:** ![nvillette](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nvillette](https://www.ruby-forum.com/u/nvillette)\
**Post date:** [March 12, 2013, 7:09pm UTC](https://www.ruby-forum.com/t/access-values-for-json-parse-response/227331/6 "2013-03-12T19:09:14Z")

</div>

Hi,

On 3/11/13 9:35 PM, Nicole V. wrote:

> puts parsed = JSON.parse(result)
> 
> Ex. How would I verify that the duration is 30?
> 
> Response:

{“TOD”:{“version”:“1.0”,“AdBreaks”:{“AdBreak”:[{“adType”:“AUDIO”,“duration”:30,“adCount”:1,“Ad”:[{“id”:611,“acc”:225,“lastModified”:“2013-03-11T19:22:15”,“order”:1,“adType”:“AUDIO”,“duration”:30,“MediaFile”:{“uri”:“http://content.dev.targetspot.com/content/act/225/adunit/86/02/1363029493701178\_225.mp3”,“type”:“audio/mpeg”,“bitrate”:64},“Impression”:{“Log”:[{“uri”:“http://nvillette.dev.targetspot.com/log.php?data=VVNFUj00YzRhN2FjZi1jMWIzLTQxMWItOTAzMy1kMTZiMzI0YTM3YzMmU1RBVD1LUkFZRk0mSUQ9NjExJkFDQ09VTlRJRD0yMjUmQURVTklUSUQ9NTEyJlBSPTI1Jk1QUj0yNSZDVD1TMzAmUkVQPTkwMCZaPTI4OTAyJk1BUktFVElEPTUyNCZTVEFURT1OQyZHRU9QUkVGSUQ9MSZSRUdJT05JRD04JkNBQ0hFQlVTVEVSPTEzNjMwMzM2ODcmQk49MyZWPVRPRCZHPTAmQT0xJkk9MCZWSVNVQUxNT0RFPTImRkxBU0hFTkFCTEVEPTAmVEhJUkRQQVJUWVNUQVRVUz0xJlRTQU5BTFlUSUNTPTEmTVNBPTAmQURTT1VSQ0VTRUxFQ1RFRD0xJkFEU09VUkNFQVRURU1QVEVEJTVCMCU1RD0xJk1FVEhPRD0yJk1PQklMRT0wJlBSRVJPTEw9MCZDTEs9MCZET1dOTE9BRD0wJkNBTExDTElDSz0wJlJBVEVMT0c9MCZJR05PUkVfR0VPX0lQPTAmVFo9QW1lcmljYSUyRk5l  
d19Zb3JrJkNUQ0Y9LTEmU1RDRj0tMSZDSUNGPS0xJkdNPVpJUCZEUz0wLjAwMDAwMDAwMDAmQURCUkVBSz0zMA==”,“event”:“complete”,“source”:“TargetSpot”}]},“CompanionVisual”:{“MediaFile”:[{“uri”:“http://content.dev.targetspot.com/content/act/225/visual/89/9f/180/135/1363029483307708\_225.jpg”,“type”:“jpg”,“width”:180,“height”:135},{“uri”:“http://content.dev.targetspot.com/content/act/225/visual/89/9f/180/150/1363029483307708\_225.jpg”,“type”:“jpg”,“width”:180,“height”:150},{“uri”:“http://content.dev.targetspot.com/content/act/225/visual/89/9f/300/250/1363029483307708\_225.jpg”,“type”:“jpg”,“width”:300,“height”:250},{“uri”:“http://content.dev.targetspot.com/content/act/225/visual/89/9f/320/240/1363029483307708\_225.jpg”,“type”:“jpg”,“width”:320,“height”:240},{“uri”:“http://content.dev.targetspot.com/content/act/225/visual/89/9f/336/280/1363029483307708\_225.jpg”,“type”:“jpg”,“width”:336,“height”:280}]}}]}]}}}

If you want to access the returned structure like you would in  
Javascript, you can use the object\_class argument with  
JSON::GenericObject like this:

> > parsed = JSON.parse result, object\_class: JSON::GenericObject  
> > […]

Then you can use method calls for the path your interested in:

---

<div class="post-metadata">

**Author:** ![nvillette](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nvillette](https://www.ruby-forum.com/u/nvillette)\
**Post date:** [March 12, 2013, 6:31pm UTC](https://www.ruby-forum.com/t/access-values-for-json-parse-response/227331/7 "2013-03-12T18:31:37Z")

</div>

Joel P. wrote in post #1101249:

> Any reason you don’t just write a recursive method? I find that’s the  
> easiest way to extract values from nested structures.

I tried but I just couldn’t seem to get it to work correctly.

Thanks.

---

<div class="post-metadata">

**Author:** ![nvillette](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nvillette](https://www.ruby-forum.com/u/nvillette)\
**Post date:** [March 12, 2013, 10:18pm UTC](https://www.ruby-forum.com/t/access-values-for-json-parse-response/227331/8 "2013-03-12T22:18:29Z")

</div>

Joel P. wrote in post #1101264:

> There’s probably a simpler way to lay this out, but this worked for me:
> 
> def recursive\_find( key, object )  
> case object  
> when Array  
> object.each do |el|  
> if el.is\_a?(Hash) || el.is\_a?(Array)  
> res = recursive\_find( key, el )  
> return res if res  
> end  
> end  
> when Hash  
> return object[key] if object.has\_key?( key )  
> object.each do |k,v|  
> if v.is\_a?(Hash) || v.is\_a?(Array)  
> res = recursive\_find( key, v )  
> return res if res  
> end  
> end  
> end  
> nil  
> end
> 
> irb(main):022:0\> recursive\_find ‘duration’, h  
> =\> 30

Joel,

I do have one question for you. What happens when there are duplicate  
names in the response. For ex. duration and uri appear in different  
arrays, so how would you specify which one you want?

---

<div class="post-metadata">

**Author:** ![nvillette](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nvillette](https://www.ruby-forum.com/u/nvillette)\
**Post date:** [March 12, 2013, 8:05pm UTC](https://www.ruby-forum.com/t/access-values-for-json-parse-response/227331/9 "2013-03-12T20:05:56Z")

</div>

There’s probably a simpler way to lay this out, but this worked for me:

def recursive\_find( key, object )  
case object  
when Array  
object.each do |el|  
if el.is\_a?(Hash) || el.is\_a?(Array)  
res = recursive\_find( key, el )  
return res if res  
end  
end  
when Hash  
return object[key] if object.has\_key?( key )  
object.each do |k,v|  
if v.is\_a?(Hash) || v.is\_a?(Array)  
res = recursive\_find( key, v )  
return res if res  
end  
end  
end  
nil  
end

irb(main):022:0\> recursive\_find ‘duration’, h  
=\> 30

---

<div class="post-metadata">

**Author:** ![nvillette](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nvillette](https://www.ruby-forum.com/u/nvillette)\
**Post date:** [March 13, 2013, 12:58am UTC](https://www.ruby-forum.com/t/access-values-for-json-parse-response/227331/10 "2013-03-13T00:58:47Z")

</div>

Nicole V. wrote in post #1101285:

> Joel P. wrote in post #1101264:
> 
> > There’s probably a simpler way to lay this out, but this worked for me:  
> > irb(main):022:0\> recursive\_find ‘duration’, h  
> > =\> 30
> 
> Joel,
> 
> I do have one question for you. What happens when there are duplicate  
> names in the response. For ex. duration and uri appear in different  
> arrays, so how would you specify which one you want?

Well, one way I thought of was to have it do a select instead of a find  
(i.e. return all the matches, not just the first). To make it useful, I  
had it return the path to the value as well as the value itself.

def recursive\_select( key, object, path= )  
matches =   
case object  
when Array  
object.each\_with\_index do |el, i|  
if el.is\_a?(Hash) || v.is\_a?(Array)  
res = recursive\_select( key, el, path+[i] )  
matches += res if res  
end  
end  
when Hash  
matches \<\< {‘path’=\>path+[key], ‘value’=\>object[key]} if  
object.has\_key? key  
object.each do |k,v|  
if v.is\_a?(Hash) || v.is\_a?(Array)  
res = recursive\_select( key, v, path+[k] )  
matches += res if res  
end  
end  
end  
matches.any? ? matches : nil  
end

irb(main):005:0\> recursive\_select ‘duration’, h  
=\> [{“path”=\>[“TOD”, “AdBreaks”, “AdBreak”, 0, “duration”],  
“value”=\>30}, {“path”=\>[“TOD”, “AdBreaks”, “AdBreak”, 0, “Ad”, 0,  
“duration”], “value”=\>30}]

---

<div class="post-metadata">

**Author:** ![nvillette](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nvillette](https://www.ruby-forum.com/u/nvillette)\
**Post date:** [March 13, 2013, 12:15pm UTC](https://www.ruby-forum.com/t/access-values-for-json-parse-response/227331/11 "2013-03-13T12:15:31Z")

</div>

By the way, I corrected this on the forum but it’s still wrong in  
Matthew’s quote and presumably the emails:

This:  
if el.is\_a?(Hash) || v.is\_a?(Array)

Should be this:  
if el.is\_a?(Hash) || el.is\_a?(Array)

---

<div class="post-metadata">

**Author:** ![nvillette](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nvillette](https://www.ruby-forum.com/u/nvillette)\
**Post date:** [March 13, 2013, 4:58pm UTC](https://www.ruby-forum.com/t/access-values-for-json-parse-response/227331/12 "2013-03-13T16:58:32Z")

</div>

Joel P. wrote in post #1101386:

> By the way, I corrected this on the forum but it’s still wrong in  
> Matthew’s quote and presumably the emails:
> 
> This:  
> if el.is\_a?(Hash) || v.is\_a?(Array)
> 
> Should be this:  
> if el.is\_a?(Hash) || el.is\_a?(Array)

Thanks for your help. I like the idea of seeing the path.

---

<div class="post-metadata">

**Author:** ![nvillette](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nvillette](https://www.ruby-forum.com/u/nvillette)\
**Post date:** [March 13, 2013, 12:21pm UTC](https://www.ruby-forum.com/t/access-values-for-json-parse-response/227331/13 "2013-03-13T12:21:44Z")

</div>

Joel P. wrote in post #1101386:

> By the way, I corrected this on the forum but it’s still wrong in  
> Matthew’s quote and presumably the emails:
> 
> This:  
> if el.is\_a?(Hash) || v.is\_a?(Array)
> 
> Should be this:  
> if el.is\_a?(Hash) || el.is\_a?(Array)

Ooh, I missed that one too. I’d fix my post, but it’s been sitting too  
long.

---

<div class="post-metadata">

**Author:** ![nvillette](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nvillette](https://www.ruby-forum.com/u/nvillette)\
**Post date:** [March 13, 2013, 9:33pm UTC](https://www.ruby-forum.com/t/access-values-for-json-parse-response/227331/14 "2013-03-13T21:33:24Z")

</div>

Nicole V. wrote in post #1101455:

> Joel P. wrote in post #1101386:
> 
> > By the way, I corrected this on the forum but it’s still wrong in  
> > Matthew’s quote and presumably the emails:
> > 
> > This:  
> > if el.is\_a?(Hash) || v.is\_a?(Array)
> > 
> > Should be this:  
> > if el.is\_a?(Hash) || el.is\_a?(Array)
> 
> Thanks for your help. I like the idea of seeing the path.

Joel,

What I need to do is verify that specific values exist in the response.  
For example, I expect the duration to be 30. So in ruby I can use the  
following:

val=recursive\_select( “duration”, parsed)  
assert\_equal(30,val,“Item NOT found”)

Your function returns the following which is great because I need to be  
able to see all of the values for duration.

value30pathTODAdBreaksAdBreak0duration  
value15pathTODAdBreaksAdBreak0Ad0duration  
value15pathTODAdBreaksAdBreak0Ad1duration

Any suggestions on how I would go about verifying that duration is ‘30’?  
or duration is ‘15’ in an assertion? Do I need to use the strip method  
to grab the values? I’m just trying to find the best way to handle this.

Thanks again for your assistance.

---

<div class="post-metadata">

**Author:** ![nvillette](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nvillette](https://www.ruby-forum.com/u/nvillette)\
**Post date:** [March 14, 2013, 1:23am UTC](https://www.ruby-forum.com/t/access-values-for-json-parse-response/227331/15 "2013-03-14T01:23:32Z")

</div>

Nicole V. wrote in post #1101490:

> Nicole V. wrote in post #1101455:  
> Joel,
> 
> What I need to do is verify that specific values exist in the response.  
> For example, I expect the duration to be 30. So in ruby I can use the  
> following:
> 
> val=recursive\_select( “duration”, parsed)  
> assert\_equal(30,val,“Item NOT found”)
> 
> Your function returns the following which is great because I need to be  
> able to see all of the values for duration.

Joel’s doesn’t. Mine does. 😉

> value30pathTODAdBreaksAdBreak0duration  
> value15pathTODAdBreaksAdBreak0Ad0duration  
> value15pathTODAdBreaksAdBreak0Ad1duration

That’s the .to\_s of what it returns. It’s actually:

[  
{“value”=\>30, “path”=\>[“TOD”,“AdBreaks”,“AdBreak”,0,“duration”]},  
{“value”=\>15,  
“path”=\>[“TOD”,“AdBreaks”,“AdBreak”,0,“Ad”,0,“duration”]},  
{“value”=\>15,  
“path”=\>[“TOD”,“AdBreaks”,“AdBreak”,0,“Ad”,1,“duration”]},  
]

> Any suggestions on how I would go about verifying that duration is ‘30’?  
> or duration is ‘15’ in an assertion? Do I need to use the strip method  
> to grab the values? I’m just trying to find the best way to handle this.
> 
> Thanks again for your assistance.

I made a gist [https://gist.github.com/phluid61/5157640](https://gist.github.com/phluid61/5157640) but I think I  
broke github.

In summary, the relevant points are:

```
# test that there are any durations
assert( !val.empty?, "Item NOT found" )

# test that _ANY_ durations are good
assert(
  val.any?{|v| v['value'] == 30 },
  "Item(s) found, but none are #{target}" )

# test that _ALL_ durations are good
val.each do |v|
  assert_equal(30, v['value'],
    "Item /#{v['path'].join '/'} = #{v['value']}" )
end

```

If it’s allowed to be 30 or 15 you might want some better logic, like:

```
val.any?{|v| [15,30].include? v['value'] }

```

and maybe

```
val.each do |v|
  assert( [15,30].include? v['value'], # ...

```

---

<div class="post-metadata">

**Author:** ![nvillette](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nvillette](https://www.ruby-forum.com/u/nvillette)\
**Post date:** [March 14, 2013, 2:06am UTC](https://www.ruby-forum.com/t/access-values-for-json-parse-response/227331/16 "2013-03-14T02:06:25Z")

</div>

Matthew K. wrote in post #1101504:

> ```
> # test that _ANY_ durations are good
> assert(
> val.any?{|v| v['value'] == 30 },
> "Item(s) found, but none are #{target}" )
> 
> ```

Oops, sorry, that should be 30 instead of #{target}

---

<div class="post-metadata">

**Author:** ![nvillette](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nvillette](https://www.ruby-forum.com/u/nvillette)\
**Post date:** [March 14, 2013, 5:42pm UTC](https://www.ruby-forum.com/t/access-values-for-json-parse-response/227331/17 "2013-03-14T17:42:54Z")

</div>

> If it’s allowed to be 30 or 15 you might want some better logic, like:
> 
> ```
> val.any?{|v| [15,30].include? v['value'] }
> 
> ```
> 
> and maybe
> 
> ```
> val.each do |v|
> assert( [15,30].include? v['value'], # ...
> 
> ```

oops..I’m getting you guys mixed up…Thanks Matthew… You guys have  
been really helpful.
