# Parsing nested JSON data

**URL:** <https://rubytalk.org/t/parsing-nested-json-data/42451>\
**Category:** ruby-talk\
**Created:** [24 November 2007 00:30 UTC](https://rubytalk.org/t/parsing-nested-json-data/42451 "2007-11-24T00:30:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marko\_Anastasov](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@Marko\_Anastasov](https://rubytalk.org/u/Marko_Anastasov)\
**Post date:** [24 November 2007 00:30 UTC](https://rubytalk.org/t/parsing-nested-json-data/42451/1 "2007-11-24T00:30:00Z")

</div>

Hi,  
I didn't find a mailing list dedicated to ruby-json, so I assume that  
this would be  
the correct place to ask.

From a web API, I receive a JSON stream such as:

[{“u”:“http://…html”,“d”:“some title”,“t”:[“ruby”]},  
{“u”:“http://…org/”,“d”:“another title”,“t”:  
[“ruby”,“json”,“library”]},…]

Now, among the rest, I also want to access those "ruby", "json" and  
"library" strings.  
With the following code:

@structs = JSON.parse(@json\_stream)

for i in 0...@structs.length  
&nbsp;&nbsp;@struct\_obj = @structs.fetch(i)  
&nbsp;&nbsp;@struct\_obj.each { |key, value|  
&nbsp;&nbsp;&nbsp;&nbsp;if key == "t"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# get the value that 't' points to  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;}  
end

I get them all appended to each other without any whitespace, ie  
"rubyjsonlibrary".

How could I access them individually?

Marko

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [24 November 2007 01:48 UTC](https://rubytalk.org/t/parsing-nested-json-data/42451/2 "2007-11-24T01:48:33Z")

</div>

Marko Anastasov wrote:

> Now, among the rest, I also want to access those "ruby", "json" and  
> "library" strings.  
> With the following code:
> 
> @structs = JSON.parse(@json\_stream)
> 
> for i in 0...@structs.length  
> &nbsp;&nbsp;@struct\_obj = @structs.fetch(i)  
> &nbsp;&nbsp;@struct\_obj.each { |key, value|  
> &nbsp;&nbsp;&nbsp;&nbsp;if key == "t"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# get the value that 't' points to  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;}  
> end
> 
> I get them all appended to each other without any whitespace, ie  
> "rubyjsonlibrary".
> 
> How could I access them individually?
> 
> Marko

> for i in 0...@structs.length  
> &nbsp;&nbsp;@struct\_obj = @structs.fetch(i)  
> &nbsp;&nbsp;@struct\_obj.each { |key, value|  
> &nbsp;&nbsp;&nbsp;&nbsp;if key == "t"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# get the value that 't' points to

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#the value that t points to is stored in your 'value' variable  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value.each do |str|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts str  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

> **···**
>
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;}  
> > end
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [24 November 2007 03:36 UTC](https://rubytalk.org/t/parsing-nested-json-data/42451/3 "2007-11-24T03:36:41Z")

</div>

7stud probably covered your question, so if you don't mind I'd like to  
hijack this thread and ask a more general question about JSON  
parsing...

Is it possible that JSON could be built into Ruby? The syntax is so  
close to Ruby's as of 1.9, that it seems a small step and rather a  
shame not to just go ahead and make it compatible. Off hand it seems  
that only quoted keys are missing.

T.

> **···**
>
> On Nov 23, 7:30 pm, Marko Anastasov \<marko.anasta...@gmail.com\> wrote:
> 
> > From a web API, I receive a JSON stream such as:
> > 
> > [{"u":"http://…html",“d”:“some title","t":["ruby"]},  
> > {"u":"http://…org/”,“d”:"another title","t":  
> > ["ruby","json","library"]},...]
> > 
> > Now, among the rest, I also want to access those "ruby", "json" and  
> > "library" strings.  
> > With the following code:
> > 
> > @structs = JSON.parse(@json\_stream)
> > 
> > for i in 0...@structs.length  
> > &nbsp;&nbsp;@struct\_obj = @structs.fetch(i)  
> > &nbsp;&nbsp;@struct\_obj.each { |key, value|  
> > &nbsp;&nbsp;&nbsp;&nbsp;if key == "t"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# get the value that 't' points to  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;}  
> > end
> > 
> > I get them all appended to each other without any whitespace, ie  
> > "rubyjsonlibrary".
> > 
> > How could I access them individually?

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [24 November 2007 04:06 UTC](https://rubytalk.org/t/parsing-nested-json-data/42451/4 "2007-11-24T04:06:06Z")

</div>

7stud -- wrote:

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#the value that t points to is stored in your 'value' variable  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value.each do |str|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts str  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

Whoops. Never mind. I'm not even sure why anyone would use the  
ruby-json gem. There's no documentation anywhere, and after I installed  
it, I couldn't even require it into a program without error.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [24 November 2007 07:05 UTC](https://rubytalk.org/t/parsing-nested-json-data/42451/5 "2007-11-24T07:05:15Z")

</div>

7stud -- wrote:

> There's no documentation anywhere, and after I installed  
> it, I couldn't even require it into a program without error.

Look how easy it is with the json gem:

require 'rubygems'  
require 'json'

str = '[  
{"u":"http://…html",“d”:“some title","t":["ruby"]},  
{"u":"http://…org/”,“d”:"another  
title","t":["ruby","json","library"]}  
]'

arr = JSON.parse(str)  
p arr  
puts

target\_hash = arr[1]  
target\_hash.each do |key, val|  
&nbsp;&nbsp;if key == 't'  
&nbsp;&nbsp;&nbsp;&nbsp;val.each {|elmt| puts elmt}  
&nbsp;&nbsp;end  
end

--output:--  
[{"d"=\>"some title", "t"=\>["ruby"], "u"=\>"http://…html"},  
{"d"=\>"another title", "t"=\>["ruby", "json", "library"],  
"u"=\>"http://…org/"}]

ruby  
json  
library

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Marko\_Anastasov](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@Marko\_Anastasov](https://rubytalk.org/u/Marko_Anastasov)\
**Post date:** [24 November 2007 11:10 UTC](https://rubytalk.org/t/parsing-nested-json-data/42451/6 "2007-11-24T11:10:04Z")

</div>

Thanks a lot 7stud. I didn't realize that each val (as in your last  
code snippet) is an array.

> **···**
>
> On Nov 24, 8:05 am, 7stud -- \<bbxx789\_0...@yahoo.com\> wrote:
> 
> > 7stud -- wrote:  
> > \> There's no documentation anywhere, and after I installed  
> > \> it, I couldn't even require it into a program without error.
> > 
> > Look how easy it is with the json gem:
> > 
> > require 'rubygems'  
> > require 'json'
> > 
> > str = '[  
> > {"u":"http://…html",“d”:“some title","t":["ruby"]},  
> > {"u":"http://…org/”,“d”:"another  
> > title","t":["ruby","json","library"]}  
> > ]'
> > 
> > arr = JSON.parse(str)  
> > p arr  
> > puts
> > 
> > target\_hash = arr[1]  
> > target\_hash.each do |key, val|  
> > &nbsp;&nbsp;if key == 't'  
> > &nbsp;&nbsp;&nbsp;&nbsp;val.each {|elmt| puts elmt}  
> > &nbsp;&nbsp;end  
> > end
> > 
> > --output:--  
> > [{"d"=\>"some title", "t"=\>["ruby"], "u"=\>"http://…html"},  
> > {"d"=\>"another title", "t"=\>["ruby", "json", "library"],  
> > "u"=\>"http://…org/"}]
> > 
> > ruby  
> > json  
> > library  
> > --  
> > Posted viahttp://www.ruby-forum.com/.
