Lee Eather
Lee Eather

Reputation: 355

Jbuilder extract json hash results using Jbuilder object?

I am trying to use Jbuilder to extract some json objects from a json array, like so

irb> photos = [{"album"=>{"created_time"=>"123", "name"=>"hello"}, {"album"=>{"created_time"=>"123", "name"=>"hello2"},........]

irb> json = Jbuilder.new
irb> json.photos photos do |photo|
irb* json.created_time photo.album.created_time
irb* end
nomethoderror: undefined method 'album' for Hash:0x65...>

Or converting to a json string using to_json string i get

nomethoderror undefined method 'map' for String:0x83....>

All I need is the data out of this so I can have each set of json results which will be saved on a User object

I haven't worked with json before and how to manipulate it, it explains in the Jbuilder readme here how to work with Jbuilder in the model but I cannot for the life of me understand it. I was trying to play around with the example but it doesn't work.

Upvotes: 0

Views: 1267

Answers (1)

Sanjay
Sanjay

Reputation: 382

I have traced your code and also tried.I get this solution so You can try this one. Make sure to define proper json object.

photos = [{"album"=>{"created_time"=>"123", "name"=>"hello"}}, {"album"=>{"created_time"=>"123", "name"=>"hello2"}}]

2.3.0 :011 > json.photos photos do |photo|
2.3.0 :012 >     json.created_time photo['album']['created_time']
2.3.0 :013?>   end

Upvotes: 1

Related Questions