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

19,461

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 }
Share:
19,461
AnApprentice
Author by

AnApprentice

working on Matter, a new way to gather professional feedback.

Updated on June 08, 2022

Comments

  • AnApprentice
    AnApprentice almost 2 years

    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