Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

given a json object, how to iterate through the object in rails

My app is posting the following json object to rails.

[{\"completed\":false,\"id\":196,\"position\":0,\"title\":\"Item 1\",\"updated_at\":\"2011-08-03T21:17:09Z\"},{\"completed\":false,\"id\":193,\"position\":1,\"title\":\"Item X\",\"updated_at\":\"2011-08-03T21:16:19Z\"},{\"completed\":false,\"id\":197,\"position\":2,\"title\":\"Item 2\",\"updated_at\":\"2011-08-03T21:17:11Z\"},{\"completed\":false,\"id\":192,\"position\":3,\"title\":\"Item Z\",\"updated_at\":\"2011-08-03T21:16:17Z\"},{\"completed\":false,\"id\":194,\"position\":4,\"title\":\"Item Y\",\"updated_at\":\"2011-08-03T21:16:21Z\"},{\"completed\":false,\"id\":195,\"position\":5,\"title\":\"Item P\",\"updated_at\":\"2011-08-03T21:16:25Z\"},{\"completed\":false,\"id\":199,\"position\":6,\"title\":\"Why you no stay done?\",\"updated_at\":\"2011-08-03T23:22:41Z\"},{\"completed\":false,\"id\":200,\"position\":7,\"title\":\"Wow\",\"updated_at\":\"2011-08-04T00:02:07Z\"},{\"completed\":false,\"id\":201,\"position\":8,\"title\":\"Hello World\",\"updated_at\":\"2011-08-04T00:02:11Z\"},{\"completed\":false,\"id\":202,\"position\":9,\"title\":\"GOLDEN GATE BRIDGE CATALYST,\",\"updated_at\":\"2011-08-04T00:44:19Z\"}]

I'm unable to loop through the object. I can do:

JSON.parse(list_items_open).each do |item|
  Rails.logger.info item.inspect
end

which outputs:

{"completed"=>false, "id"=>193, "position"=>0, "title"=>"Item X", "updated_at"=>"2011-08-03T21:16:19Z"}
{"completed"=>false, "id"=>197, "position"=>1, "title"=>"Item 2", "updated_at"=>"2011-08-03T21:17:11Z"}
{"completed"=>false, "id"=>192, "position"=>2, "title"=>"Item Z", "updated_at"=>"2011-08-03T21:16:17Z"}
{"completed"=>false, "id"=>196, "position"=>3, "title"=>"Item 1", "updated_at"=>"2011-08-03T21:17:09Z"}
{"completed"=>false, "id"=>194, "position"=>4, "title"=>"Item Y", "updated_at"=>"2011-08-03T21:16:21Z"}
{"completed"=>false, "id"=>195, "position"=>5, "title"=>"Item P", "updated_at"=>"2011-08-03T21:16:25Z"}
{"completed"=>false, "id"=>199, "position"=>6, "title"=>"Why you no stay done?", "updated_at"=>"2011-08-03T23:22:41Z"}
{"completed"=>false, "id"=>200, "position"=>7, "title"=>"Wow", "updated_at"=>"2011-08-04T00:02:07Z"}
{"completed"=>false, "id"=>201, "position"=>8, "title"=>"Hello World", "updated_at"=>"2011-08-04T00:02:11Z"}
{"completed"=>false, "id"=>202, "position"=>9, "title"=>"GOLDEN GATE BRIDGE CATALYST,", "updated_at"=>"2011-08-04T00:44:19Z"}

But I can't get the actual id:

JSON.parse(list_items_open).each do |item|
  Rails.logger.info item[:id]
end

That returns nothing. Suggestions on how to get the item ID?

Thanks

like image 893
AnApprentice Avatar asked Aug 04 '11 03:08

AnApprentice


1 Answers

Symbols and strings are different things (except when you're using HashWithIndifferentAccess), you should be using a string as a key:

JSON.parse(list_items_open).each do |item|
  Rails.logger.info item['id']
end

This is a hash with an 'id' key:

{ "id" => 193 }

This is a hash with an :id key:

{ :id => 193 }
like image 200
mu is too short Avatar answered Oct 05 '22 00:10

mu is too short