# How to use json in view file

**URL:** <https://www.ruby-forum.com/t/how-to-use-json-in-view-file/167189>\
**Category:** Rails\
**Created:** [May 5, 2009, 9:31am UTC](https://www.ruby-forum.com/t/how-to-use-json-in-view-file/167189 "2009-05-05T09:31:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrbless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ba9def/32.png) [@mrbless](https://www.ruby-forum.com/u/mrbless)\
**Post date:** [May 5, 2009, 9:31am UTC](https://www.ruby-forum.com/t/how-to-use-json-in-view-file/167189/1 "2009-05-05T09:31:59Z")

</div>

In my controller I have found all the movies like this  
@budgets=Budget.find(:all).to\_json

Now in the view file what i want to do is something like this

How to do this as i have to plot this data.

---

<div class="post-metadata">

**Author:** ![mrbless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ba9def/32.png) [@mrbless](https://www.ruby-forum.com/u/mrbless)\
**Post date:** [May 5, 2009, 9:44am UTC](https://www.ruby-forum.com/t/how-to-use-json-in-view-file/167189/2 "2009-05-05T09:44:53Z")

</div>

On Tuesday 05 May 2009, mrbless wrote:

> In my controller I have found all the movies like this  
> @budgets=Budget.find(:all).to\_json

If it’s ok to issue a separate request for the data, have a look at

> **[GitHub - mschuerig/ruby\_template\_handler: A Rails view template handler that...](https://github.com/mschuerig/ruby_template_handler)**
>
> A Rails view template handler that renders Ruby objects to JSON. - GitHub - mschuerig/ruby\_template\_handler: A Rails view template handler that renders Ruby objects to JSON.

One way or another, you’ll have to do the conversion to the specific  
format you need yourself.

Michael

> ```
> },
> "russia": {
> label: "Russia",
> data: [[1988, 218000], [1989, 203000], [1990, 171000],
> 
> ```
> 
> [1992, 42500], [1993, 37600], [1994, 36600], [1995, 21700], [1996,  
> 19200], [1997, 21300], [1998, 13600], [1999, 14000], [2000, 19100],  
> [2001, 21300], [2002, 23600], [2003, 25100], [2004, 26100], [2005,  
> 31100], [2006, 34700]]  
> }  
> }

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)

---

<div class="post-metadata">

**Author:** ![mrbless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ba9def/32.png) [@mrbless](https://www.ruby-forum.com/u/mrbless)\
**Post date:** [May 5, 2009, 1:49pm UTC](https://www.ruby-forum.com/t/how-to-use-json-in-view-file/167189/3 "2009-05-05T13:49:48Z")

</div>

mrbless wrote:

> In my controller I have found all the movies like this  
> @budgets=Budget.find(:all).to\_json
> 
> Now in the view file what i want to do is something like this

What is the output if you write:

p @bugets

in the controller?

---

<div class="post-metadata">

**Author:** ![mrbless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ba9def/32.png) [@mrbless](https://www.ruby-forum.com/u/mrbless)\
**Post date:** [May 5, 2009, 3:35pm UTC](https://www.ruby-forum.com/t/how-to-use-json-in-view-file/167189/4 "2009-05-05T15:35:30Z")

</div>

=\> [#\<Budget id: 1, country: “usa”, year: “2000”, amount: 15000\>,  
#\<Budget id: 2  
, country: “usa”, year: “2001”, amount: 1564\>, #\<Budget id: 3,  
country: “usa”, y  
ear: “2002”, amount: 6463\>, #\<Budget id: 4, country: “usa”, year:  
“2003”, amount  
: 150002\>, #\<Budget id: 5, country: “usa”, year: “2004”, amount:  
5000\>, #\<Budget  
id: 6, country: “usa”, year: “2005”, amount: 4150\>, #\<Budget id: 7,  
country: “u  
sa”, year: “2006”, amount: 100\>, #\<Budget id: 8, country: “germany”,  
year: "2000  
", amount: 1000\>, #\<Budget id: 9, country: “germany”, year: “2001”,  
amount: 1526  
4\>, #\<Budget id: 10, country: “germany”, year: “2002”, amount: 64633\>,  
#\<Budget  
id: 11, country: “germany”, year: “2003”, amount: 1502\>, #\<Budget id:  
12, countr  
y: “germany”, year: “2004”, amount: 500\>, #\<Budget id: 13, country:  
“germany”, y  
ear: “2005”, amount: 450\>, #\<Budget id: 14, country: “germany”, year:  
“2006”, am  
ount: 10\>, #\<Budget id: 15, country: “britian”, year: “2000”, amount:  
150\>, #\<Bu  
dget id: 16, country: “britian”, year: “2001”, amount: 15064\>,  
#\<Budget id: 17,  
country: “britian”, year: “2002”, amount: 663\>, #\<Budget id: 18,  
country: “briti  
an”, year: “2003”, amount: 2002\>, #\<Budget id: 19, country: “britian”,  
year: “20  
04”, amount: 50000\>, #\<Budget id: 20, country: “britian”, year:  
“2005”, amount:  
4150\>, #\<Budget id: 21, country: “britian”, year: “2006”, amount:  
1000\>]

---

<div class="post-metadata">

**Author:** ![mrbless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ba9def/32.png) [@mrbless](https://www.ruby-forum.com/u/mrbless)\
**Post date:** [May 6, 2009, 7:11am UTC](https://www.ruby-forum.com/t/how-to-use-json-in-view-file/167189/5 "2009-05-06T07:11:24Z")

</div>

Thank you very much.

---

<div class="post-metadata">

**Author:** ![mrbless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ba9def/32.png) [@mrbless](https://www.ruby-forum.com/u/mrbless)\
**Post date:** [May 5, 2009, 6:02pm UTC](https://www.ruby-forum.com/t/how-to-use-json-in-view-file/167189/6 "2009-05-05T18:02:41Z")

</div>

nirvana wrote:

> =\> [#\<Budget id: 1, country: “usa”, year: “2000”, amount: 15000\>,  
> #\<Budget id: 2  
> , country: “usa”, year: “2001”, amount: 1564\>, #\<Budget id: 3,  
> country: “usa”, y  
> ear: “2002”, amount: 6463\>, #\<Budget id: 4, country: “usa”, year:  
> “2003”, amount  
> : 150002\>, #\<Budget id: 5, country: “usa”, year: “2004”, amount:  
> 5000\>, #\<Budget  
> id: 6, country: “usa”, year: “2005”, amount: 4150\>, #\<Budget id: 7,  
> country: “u  
> sa”, year: “2006”, amount: 100\>, #\<Budget id: 8, country: “germany”,  
> year: "2000  
> ", amount: 1000\>, #\<Budget id: 9, country: “germany”, year: “2001”,  
> amount: 1526  
> 4\>, #\<Budget id: 10, country: “germany”, year: “2002”, amount: 64633\>,  
> #\<Budget  
> id: 11, country: “germany”, year: “2003”, amount: 1502\>, #\<Budget id:  
> 12, countr  
> y: “germany”, year: “2004”, amount: 500\>, #\<Budget id: 13, country:  
> “germany”, y  
> ear: “2005”, amount: 450\>, #\<Budget id: 14, country: “germany”, year:  
> “2006”, am  
> ount: 10\>, #\<Budget id: 15, country: “britian”, year: “2000”, amount:  
> 150\>, #\<Bu  
> dget id: 16, country: “britian”, year: “2001”, amount: 15064\>,  
> #\<Budget id: 17,  
> country: “britian”, year: “2002”, amount: 663\>, #\<Budget id: 18,  
> country: “briti  
> an”, year: “2003”, amount: 2002\>, #\<Budget id: 19, country: “britian”,  
> year: “20  
> 04”, amount: 50000\>, #\<Budget id: 20, country: “britian”, year:  
> “2005”, amount:  
> 4150\>, #\<Budget id: 21, country: “britian”, year: “2006”, amount:  
> 1000\>]

require ‘pp’  
#pretty printer

class Budget  
attr\_reader :country, :year, :amount

def initialize(c, y, a)  
@country, @year, @amount = c, y, a  
end  
end

arr = [  
Budget.new(“usa”, “1989”, 15000),  
Budget.new(“usa”, “1964”, 20000),  
Budget.new(“russia”, “1999”, 14000)  
]

pp arr

–output:–  
[#\<Budget:0x8744c @amount=15000, @country=“usa”, @year=“1989”\>,  
#\<Budget:0x873fc @amount=20000, @country=“usa”, @year=“1964”\>,  
#\<Budget:0x873ac @amount=14000, @country=“russia”, @year=“1999”\>]

hash = Hash.new {|hash, key| hash[key] = []}  
#create a hash where acessing a non-existent key  
#creates the key and assigns an empty array to it

arr.each do |budget|  
hash[budget.country] \<\< [budget.year.to\_i, budget.amount]  
end

p hash

–output:–  
{“usa”=\>[[1989, 15000], [1964, 20000]], “russia”=\>[[1999, 14000]]}

str = “var datasets = {\n”

hash.each do |key, val|  
fragment =\<\<ENDOFSTRING2  
“#{key}”: {  
label: “#{key.upcase}”,  
data: #{val.inspect}  
},  
ENDOFSTRING2  
str \<\< fragment  
end  
#inspect produces the same output as p var

result = str[0…-3] + “\n}”  
#gets rid comma(and newline) after last  
#element, then adds the closing parenthesis

puts result

–output:–  
var datasets = {  
“usa”: {  
label: “USA”,  
data: [[1989, 15000], [1964, 20000]]  
},  
“russia”: {  
label: “RUSSIA”,  
data: [[1999, 14000]]  
}  
}

You’ll have to manipulate the label to get the exact format you need.

Of course, you wouldn’t put all that code in a view.
