Morez SA Morez SA - 7 months ago 46
JSON Question

Extract specific JSON hash information to Rails view

I'm trying to implement Bitcoin payments using, and when I request a balance from the bitcoin address, it responds with a hash. How can I extract specific information and make the data user friendly.

The hash I need to extract the information from looks like this:

{"status"=>"success", "data"=>{"network"=>"BTCTEST", "available_balance"=>"0.01000000", "pending_received_balance"=>"0.00000000"}}

I have a controller with method:

class PaymentsController < ApplicationController
def index
@balance = BlockIo.get_balance

And in the view I have:

<%= @balance.each do |bal| %>
<p>Balance: <%= bal[1]["available_balance"] %></p>
<% end %>

And the result that I get in the index.html.erb view is like this:


Balance: 0.01000000

{"status"=>"success", "data"=>{"network"=>"BTCTEST", "available_balance"=>"0.01000000", "pending_received_balance"=>"0.00000000"}}

As you can see above, it shows Balance twice and also still shows the hash in the view.

How can I only show Balance: 0.01000000?
Any assistance or comment is greatly appreciated.


First, this is Hash, not Array:

{"status"=>"success", "data"=>{"network"=>"BTCTEST",  "available_balance"=>"0.01000000", "pending_received_balance"=>"0.00000000"}} 

Second, you can do this in your view file:

<p>Balance: <%= @balance["data"]["available_balance"] %></p>