# Weird error in Hash.to\_json

**URL:** <https://www.ruby-forum.com/t/weird-error-in-hash-to-json/147988>\
**Category:** Rails\
**Created:** [September 8, 2008, 4:11am UTC](https://www.ruby-forum.com/t/weird-error-in-hash-to-json/147988 "2008-09-08T04:11:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Travers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/258eb7/32.png) [@Mike\_Travers](https://www.ruby-forum.com/u/Mike_Travers)\
**Post date:** [September 8, 2008, 4:11am UTC](https://www.ruby-forum.com/t/weird-error-in-hash-to-json/147988/1 "2008-09-08T04:11:55Z")

</div>

> > {}.to\_json()  
> > =\> “{}”
> 
> > {}.to\_json({})  
> > TypeError: wrong argument type Hash (expected Data)  
> > from (irb):4:in `to\_json’  
> > from (irb):4
> 
> >

This is in Ruby 1.8.6, Rails 2.1.1…but for some reason reloading the  
source fixes it:

> > load ‘/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active\_support/json/encoders/hash.rb’  
> > =\> []
> 
> > {}.to\_json({})  
> > =\> “{}”
> 
> >

I’m baffled…what’s a “Data” and why should to\_json be expecting one?

---

<div class="post-metadata">

**Author:** ![Mike\_Travers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/258eb7/32.png) [@Mike\_Travers](https://www.ruby-forum.com/u/Mike_Travers)\
**Post date:** [September 8, 2008, 11:18am UTC](https://www.ruby-forum.com/t/weird-error-in-hash-to-json/147988/2 "2008-09-08T11:18:16Z")

</div>

On 8 Sep 2008, at 03:10, Mike Travers wrote:

> source fixes it:
> 
> > > load ‘/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/  
> > > active\_support/json/encoders/hash.rb’  
> > > =\> []
> > 
> > > {}.to\_json({})  
> > > =\> “{}”
> > 
> > >
> 
> I’m baffled…what’s a “Data” and why should to\_json be expecting one?

I vaguely recall seeing something similar if you have one of the other  
libraries around that provide json conversions - they and  
ActiveSupport both try and define to\_json and things end up in a mess  
when they both try and do it.

Fred

---

<div class="post-metadata">

**Author:** ![Mike\_Travers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/258eb7/32.png) [@Mike\_Travers](https://www.ruby-forum.com/u/Mike_Travers)\
**Post date:** [September 8, 2008, 7:35pm UTC](https://www.ruby-forum.com/t/weird-error-in-hash-to-json/147988/3 "2008-09-08T19:35:40Z")

</div>

On Sep 7, 7:10 pm, Mike Travers [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This is in Ruby 1.8.6, Rails 2.1.1…but for some reason reloading the  
> source fixes it:
> 
> > > load ‘/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active\_support/json/encoders/hash.rb’  
> > > =\> []  
> > > {}.to\_json({})  
> > > =\> “{}”
> 
> I’m baffled…what’s a “Data” and why should to\_json be expecting one?

I found that including a line  
require ‘json/pure’  
fixes it…apparently the regular json package (which uses a C  
library) is buggy.
