# How to specify ActiveRecord's to\_json encoding

**URL:** <https://www.ruby-forum.com/t/how-to-specify-activerecords-to-json-encoding/173400>\
**Category:** Rails\
**Created:** [August 8, 2009, 1:12am UTC](https://www.ruby-forum.com/t/how-to-specify-activerecords-to-json-encoding/173400 "2009-08-08T01:12:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubynewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/58f4c7/32.png) [@rubynewbie](https://www.ruby-forum.com/u/rubynewbie)\
**Post date:** [August 8, 2009, 1:12am UTC](https://www.ruby-forum.com/t/how-to-specify-activerecords-to-json-encoding/173400/1 "2009-08-08T01:12:52Z")

</div>

Question:  
Hi, our company is using Ruby 1.8.6 with Rails 2.2.2. Does anyone  
know we can explicitly specify what encoding to use when calling  
to\_json() or to\_xml() methods?

Problem:  
We have some multibyte characters in our database. For example we  
have a table with a name column that has this French accented e: Café  
Records. When we serialize this object using ActiveRecord’s to\_xml()  
everything looks fine in the browser. When we render JSON using  
to\_json() we are seeing problems where the accented ‘e’ character is  
getting mangled and causes our calling web client to fail since it’s  
expecting properly UTF-8 encoded characters.

If we use the browser to submit HTTP Get requesting JSON format, save  
the file and view it in binary mode in Hexadecimal representation,  
this is what we get. It looks like this is using extended ASCII.

Bytes Text  
43 61 66 E9 C a f (should be accented e)

If we save that same file from above and specify UTF-8, we get an  
extra byte that seems to be proper UTF-8 encoding as shown below.

Bytes Text  
43 61 66 C3 A9 C a f (should be accented e)

Can someone tell me how they’ve made to\_json() UTF-8 compliant?  
Thanks in advance, Calvin.
