# To\_json of hash containing an array

**URL:** <https://www.ruby-forum.com/t/to-json-of-hash-containing-an-array/146714>\
**Category:** Rails\
**Created:** [August 24, 2008, 2:43pm UTC](https://www.ruby-forum.com/t/to-json-of-hash-containing-an-array/146714 "2008-08-24T14:43:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mads\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/54ee81/32.png) [@Mads\_K](https://www.ruby-forum.com/u/Mads_K)\
**Post date:** [August 24, 2008, 2:43pm UTC](https://www.ruby-forum.com/t/to-json-of-hash-containing-an-array/146714/1 "2008-08-24T14:43:42Z")

</div>

Hi there,

I am fairly new to the Rails framework, so forgive me if there is an  
obvious solution to this problem.

I am using Rails 2.1.0 and trying to jsonify a hash containing an  
array. E.g.

result = { :name =\> “McLovin”, :attachments =\> [‘file.pdf’,  
‘file.jpg’] }  
=\> {:attachments=\>[“file.pdf”, “file.jpg”], :name=\>“McLovin”}  
result.to\_json

I would expect something like:

{  
“name”: “McLovin”,  
“attachments”: [  
“file.pdf”,  
“file.jpg”  
]  
}

But instead I get:

TypeError: wrong argument type Hash (expected Data)  
from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/  
active\_support/json/encoding.rb:21:in `to_json' from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/ active_support/json/encoding.rb:21:in`send’  
from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/  
active\_support/json/encoding.rb:21:in `encode' from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/ active_support/json/encoding.rb:31:in`raise\_on\_circular\_reference’  
from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/  
active\_support/json/encoding.rb:20:in `encode' from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/ active_support/json/encoders/hash.rb:42:in`to\_json’  
from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/  
active\_support/json/encoders/hash.rb:41:in `map' from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/ active_support/json/encoders/hash.rb:41:in`to\_json’  
from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/  
active\_support/core\_ext/object/misc.rb:28:in `returning' from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/ active_support/json/encoders/hash.rb:40:in`to\_json’  
from (irb):54

If I do “result.to\_json( :except =\> :attachments )”, then it works.

Best regards, Mads K.

---

<div class="post-metadata">

**Author:** ![Mads\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/54ee81/32.png) [@Mads\_K](https://www.ruby-forum.com/u/Mads_K)\
**Post date:** [August 24, 2008, 7:44pm UTC](https://www.ruby-forum.com/t/to-json-of-hash-containing-an-array/146714/2 "2008-08-24T19:44:43Z")

</div>

On 24 Aug 2008, at 13:41, Mads K. wrote:

> ‘file.jpg’] }  
> ]  
> }  
> Which is exactly what I got when I tried this out. Do you have any  
> plugins/use any gems that could be clashing with activesupport’s JSON  
> stuff?

Fred

---

<div class="post-metadata">

**Author:** ![Mads\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/54ee81/32.png) [@Mads\_K](https://www.ruby-forum.com/u/Mads_K)\
**Post date:** [October 15, 2008, 10:23pm UTC](https://www.ruby-forum.com/t/to-json-of-hash-containing-an-array/146714/3 "2008-10-15T22:23:42Z")

</div>

This is also a problem in Merb. I believe AR’s json and Merb’s json  
don’t play well together. I put this in init.rb and now life is grand.

Merb.disable :json

On Aug 24, 1:04 pm, Mads K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Mads\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/54ee81/32.png) [@Mads\_K](https://www.ruby-forum.com/u/Mads_K)\
**Post date:** [August 24, 2008, 8:05pm UTC](https://www.ruby-forum.com/t/to-json-of-hash-containing-an-array/146714/4 "2008-08-24T20:05:46Z")

</div>

Okay, thanks. Just wanted to confirm that it actually worked  
somewhere… 🙂

I removed the “json\_pure” gem and now it works for me too… Not sure  
if that will cause other problems, but we’ll see about that.

Best regards, Mads K.
