Wessel Bosman
Wessel Bosman

Reputation: 25

Parse JSON and display values in Rails

I have some JSON saved ina database that I am retrieving and parsing but for some reason cannot loop through the parsed JSON and return it's values.

<% @submission.each do |submission| %>
 <div class="field">
  <p class="submissionDate">
    <% @dog = JSON.parse(submission.self_interests) %> #Gets the JSON from the self_interests field
      <% @dog.each do |self_interests| %>
        <%= self_interests.company_name %>  #trying to get the company_name from the parsed array
     <% end %>
  </p>
<% end %>

if I use <%= self_interests %> instead of <%= self_interests.company_name %> it outputs the parsed array as expected.

{"self_interest"=>{"appointment_date"=>"2012-07-19", "company_name"=>"asdasd", "company_registration"=>"asdas", "created_at"=>"2012-07-18T15:49:33+02:00", "date_deleted"=>nil, "date_registered"=>"2012-07-10", "date_terminated"=>"2012-07-27", "id"=>16, "trading_name"=>"asdasd", "transacting_with"=>1, "type_of_business"=>"asdasdasd", "updated_at"=>"2012-07-18T15:49:33+02:00", "user_id"=>2}}

Any help with this will be much appreciated, its been wracking my brain hard. I am quite new to Rails so it might be something obvious I am missing.

Upvotes: 2

Views: 440

Answers (1)

abhas
abhas

Reputation: 5213

try this updated one

<%= self_interests["self_interest"]["company_name"] %>

Upvotes: 1

Related Questions