From: tamouse mailing lists Date: 2013-03-12T12:29:38+09:00 Subject: Re: Access values for JSON.parse response --f46d04088f17077a5204d7b1e6ae Content-Type: text/plain; charset=UTF-8 On Mar 11, 2013 4:16 PM, "Matthew Kerwin" wrote: > > Nicole Villette 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. These are the vagaries of XML :) > lengths_of_breaks = adbreaks.map {|ad| ad['duration'] } > list_of_30s_breaks = adbreaks.select {|ad| ad['duration'] == 30 } > # etc. > > -- > Posted via http://www.ruby-forum.com/. > --f46d04088f17077a5204d7b1e6ae Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable


On Mar 11, 2013 4:16 PM, "Matthew Kerwin" <lists@ruby-forum.com> wrote:
>
> Nicole Villette wrote in post #1101133:
> > Hello, Does anyone know [how] to get the values from a nested has= h 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. =C2=A0See below for response.<= br> >
> Since your "parsed" object is a Hash, you can access the fir= st level of
> nesting with:
> =C2=A0 parsed['TOD']
> which returns a Hash, so you can access the next level with:
> =C2=A0 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:
>
> =C2=A0 parsed['TOD']['AdBreaks']['AdBreak'][0]= ['duration']
> =C2=A0 # or
> =C2=A0 parsed['TOD']['AdBreaks']['AdBreak'].fi= rst['duration']
>
> But personally I'd probably go for one of the following:
>
> =C2=A0 parsed['TOD']['AdBreaks']['AdBreak'].ma= p{|ad|
> ad['duration'] }
> =C2=A0 # or
> =C2=A0 parsed['TOD']['AdBreaks']['AdBreak'].an= y?{|ad|
> ad['duration'] =3D=3D 30 }
> =C2=A0 # or
> =C2=A0 parsed['TOD']['AdBreaks']['AdBreak'].se= lect{|ad|
> ad['duration'] =3D=3D 30 }
>
> Also note: this is yucky. =C2=A0Yucky yucky yucky. =C2=A0A saner perso= n might use
> some local variables. =C2=A0Even one will help a lot:
>
> =C2=A0 adbreaks =3D parsed['TOD']['AdBreaks']['AdB= reak']
> =C2=A0 # while I'm here: why does 'AdBreaks' hold one item= ,
> =C2=A0 # but 'AdBreak' hold an array? =C2=A0Odd.

These are the vagaries of XML :)

> =C2=A0 lengths_of_breaks =3D adbreaks.map {|ad| ad['duration= 9;] }
> =C2=A0 list_of_30s_breaks =3D adbreaks.select {|ad| ad['duration&#= 39;] =3D=3D 30 }
> =C2=A0 # etc.
>
> --
> Posted via http://www.ruby-foru= m.com/.
>

--f46d04088f17077a5204d7b1e6ae--