Mohammed
Mohammed

Reputation: 575

Iterate obect in view, JSON hash

I have parsed the JSON api externally and now it is a hash as such:

["statement", {"generated"=>"2015-01-11", "due"=>"2015-01-25", "period"=>{"from"=>"2015-01-26", "to"=>"2015-02-25"}}

I used:

require 'json/pure'
require 'open-uri'

 def index
   content = open("MY_URL").read
   @hash = JSON.parse content
   render @hash
 end

and this gives me the whole hash output on the view using:

<% @hash.each do |hash| %>
<%= hash %>
<% end %>

So lets say I wanted to print generated, due and period, separately, how would I do this. I'm so close I know it.

array:

 ["statement", {"generated"=>"2015-01-11", "due"=>"2015-01-25", "period"=>{"from"=>"2015-01-26", "to"=>"2015-02-25"}}] ["total", 136.03] ["package", {"subscriptions"=>[{"type"=>"tv", "name"=>"Variety with Movies", "cost"=>50.0}, {"type"=>"talk", "name"=>"Talk Anytime", "cost"=>5.0}, {"type"=>"broadband", "name"=>"Fibre Unlimited", "cost"=>16.4}], "total"=>71.4}] ["callCharges", {"calls"=>[{"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"02074351359", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"02074351359", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"02074351359", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"02074351359", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"02074351359", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"02074351359", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"02074351359", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"02074351359", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"02074351359", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"02074351359", "duration"=>"00:23:03", "cost"=>2.13}], "total"=>59.64}] ["Store", {"rentals"=>[{"title"=>"50 Shades of Grey", "cost"=>4.99}], "buyAndKeep"=>[{"title"=>"That's what she said", "cost"=>9.99}, {"title"=>"Broke back mountain", "cost"=>9.99}], "total"=>24.97}] [:prefixes, ["tasks", "application"]] [:template, "index"]

Upvotes: 2

Views: 569

Answers (1)

La-comadreja
La-comadreja

Reputation: 5755

In a nutshell, you need to extract the values from the hash to print them individually.

The first piece of code you wrote is actually an array, not a hash. The first element of the array is a String and the second is a hash. The first thing you want to do is create a variable with the hash only, removing the string:

<% @hash_values = @hash[1] %>

With this hash, you want to extract the values of 'generated', 'due' and 'period' by looking them up by their keys:

<%= @hash_values['generated'] %>
<%= @hash_values['due'] %>
<%= @hash_values['period'] %>

In the case of 'period', the result is a hash. You many want to extract the individual values a bit more for formatting, e.g.:

<%= @hash_values['period']['from'] %> to <%= @hash_values['period']['to'] %>

Upvotes: 1

Related Questions