From: Matthew Kerwin Date: 2013-03-14T09:23:35+09:00 Subject: Re: Access values for JSON.parse response Nicole Villette wrote in post #1101490: > Nicole Villette 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 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'], # ... -- Posted via http://www.ruby-forum.com/.