# ActiveSupport JSON working?

**URL:** <https://www.ruby-forum.com/t/activesupport-json-working/95313>\
**Category:** Rails\
**Created:** [May 27, 2006, 11:18pm UTC](https://www.ruby-forum.com/t/activesupport-json-working/95313 "2006-05-27T23:18:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [May 27, 2006, 11:18pm UTC](https://www.ruby-forum.com/t/activesupport-json-working/95313/1 "2006-05-27T23:18:52Z")

</div>

Hi,

I am trying to convert a hash to JSON using ActiveSupports built in  
.to\_json. I am doing similar to the example below but are there  
certain strings that could go in place of ‘bar’ or ‘rez’ that will  
break the to\_json method? I am using a very long string from rjs  
output in place of ‘bar’.

Perhaps .to\_json is not ready yet?

Thanks,  
Peter

def MyController \< ApplicationController  
def give\_me\_json  
# make sure not to send html but text/plain  
@headers[“Content-Type”] = “text/plain; charset=utf-8”

```
data = { :foo => 'bar', :etc => 'rez' }
render_text data.to_json

```

end  
end

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [May 28, 2006, 3:23am UTC](https://www.ruby-forum.com/t/activesupport-json-working/95313/2 "2006-05-28T03:23:58Z")

</div>

On 5/27/06, Peter M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks,  
> end  
> end

Does it break on characters that require utf-8 encoding? If so, it  
might be that your controller-file isn’t in utf-8 itself, or that it’s  
not stored as utf-8 in the database (in case you grab the data from  
there). I’ve been using the to\_json function a bit myself and it seems  
to work properly on everything I’ve tried (once I got everything utf-8  
encoded so it worked with the Swedish characters I tossed at it).

Hope that helps,  
Mathias.

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [May 28, 2006, 8:01am UTC](https://www.ruby-forum.com/t/activesupport-json-working/95313/3 "2006-05-28T08:01:59Z")

</div>

On May 27, 2006, at 6:21 PM, Mathias W. wrote:

> > ```
> > render_text data.to_json
> > 
> > ```
> 
> Hope that helps,  
> Mathias.

```
I could be totally wrong here, but aren't you supposed to send json

```

as text/javascript instead of text/plain?

-Ezra

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [May 28, 2006, 8:11am UTC](https://www.ruby-forum.com/t/activesupport-json-working/95313/4 "2006-05-28T08:11:15Z")

</div>

On 5/27/06, Ezra Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> I could be totally wrong here, but aren't you supposed to send json
> 
> ```
> 
> as text/javascript instead of text/plain?

I think that if you did return as type=text/javascript then  
Prototype.js would automatically eval() the response. But since JSON  
is just an object notation, Prototype would create an object, assign  
it to nothing and nothing would happen. It’s my guess.

Peter

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [May 28, 2006, 8:14am UTC](https://www.ruby-forum.com/t/activesupport-json-working/95313/5 "2006-05-28T08:14:22Z")

</div>

On 5/27/06, Mathias W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Does it break on characters that require utf-8 encoding? If so, it  
> might be that your controller-file isn’t in utf-8 itself, or that it’s  
> not stored as utf-8 in the database (in case you grab the data from  
> there). I’ve been using the to\_json function a bit myself and it seems  
> to work properly on everything I’ve tried (once I got everything utf-8  
> encoded so it worked with the Swedish characters I tossed at it).

Thanks for the response. Partly I asked because I wasn’t sure if  
anyone used the .to\_json from ActiveSupport yet. In the online docs I  
don’t see anything so I wasn’t sure if it was ready. Since you use it  
successfully I thought a little harder and I think I was the problem,  
of course.

Thanks  
Peter

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [May 28, 2006, 8:11am UTC](https://www.ruby-forum.com/t/activesupport-json-working/95313/6 "2006-05-28T08:11:08Z")

</div>

On 5/27/06, Peter M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> I am trying to convert a hash to JSON using ActiveSupports built in  
> .to\_json. I am doing similar to the example below but are there  
> certain strings that could go in place of ‘bar’ or ‘rez’ that will  
> break the to\_json method? I am using a very long string from rjs  
> output in place of ‘bar’.
> 
> Perhaps .to\_json is not ready yet?

I think I was trying to overcomplicate things. I now doubt any  
problems are with ActiveSupport’s JSON support. I found a nice  
solution using XML and may look back at this problem in the future.  
Thanks to those that read.

Peter
