Parsing nested JSON data

M

Marko Anastasov

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 (e-mail address removed)
@struct_obj = @structs.fetch(i)
@struct_obj.each { |key, value|
if key == "t"
# get the value that 't' points to
end
}
end

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

How could I access them individually?

Marko
 
7

7stud --

Marko said:
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 (e-mail address removed)
@struct_obj = @structs.fetch(i)
@struct_obj.each { |key, value|
if key == "t"
# get the value that 't' points to
end
}
end

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

How could I access them individually?

Marko
for i in (e-mail address removed)
@struct_obj = @structs.fetch(i)
@struct_obj.each { |key, value|
if key == "t"
# get the value that 't' points to

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

Trans

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 (e-mail address removed)
@struct_obj = @structs.fetch(i)
@struct_obj.each { |key, value|
if key == "t"
# get the value that 't' points to
end
}
end

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

How could I access them individually?

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.
 
7

7stud --

7stud said:
#the value that t points to is stored in your 'value' variable
value.each do |str|
puts str
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.
 
7

7stud --

7stud said:
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|
if key == 't'
val.each {|elmt| puts elmt}
end
end


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

ruby
json
library
 
M

Marko Anastasov

7stud said:
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|
if key == 't'
val.each {|elmt| puts elmt}
end
end

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

ruby
json
library

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top