# Ruby json library trouble

**URL:** <https://www.ruby-forum.com/t/ruby-json-library-trouble/157752>\
**Category:** Ruby\
**Created:** [January 15, 2009, 12:50am UTC](https://www.ruby-forum.com/t/ruby-json-library-trouble/157752 "2009-01-15T00:50:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lyes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ed8c4c/32.png) [@lyes](https://www.ruby-forum.com/u/lyes)\
**Post date:** [January 15, 2009, 12:50am UTC](https://www.ruby-forum.com/t/ruby-json-library-trouble/157752/1 "2009-01-15T00:50:09Z")

</div>

Hi everybody

I’m trying to export some data in the Json format using the json\_pure  
library

I’ve well prepared my structure in my program and I used the generate  
method  
to get the Jon output. But, during the generation, an error occurs  
saying  
that it met a character that is not utf-8 encoded.?

Do this mean that I’m obliged to give UTF-8 encoded strings in my  
objects to  
get my json output, and if it is the case, do you now good ruby utf-8  
encoding library to advise it to me?

Thank you!

---

<div class="post-metadata">

**Author:** ![lyes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ed8c4c/32.png) [@lyes](https://www.ruby-forum.com/u/lyes)\
**Post date:** [January 16, 2009, 9:52am UTC](https://www.ruby-forum.com/t/ruby-json-library-trouble/157752/2 "2009-01-16T09:52:30Z")

</div>

I did not check, but probably you need to use utf8 strings, at least  
for ruby 1.8

For encoding into utf8, you need to know what is your string’s current  
encoding, and then change the string into utf8 encoding. You can use  
iconv() (google ruby iconv) for that change.

izidor

---

<div class="post-metadata">

**Author:** ![lyes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ed8c4c/32.png) [@lyes](https://www.ruby-forum.com/u/lyes)\
**Post date:** [January 16, 2009, 12:04pm UTC](https://www.ruby-forum.com/t/ruby-json-library-trouble/157752/3 "2009-01-16T12:04:17Z")

</div>

Amazouz Loui wrote:

> I’ve well prepared my structure in my program and I used the generate  
> method  
> to get the Jon output. But, during the generation, an error occurs  
> saying  
> that it met a character that is not utf-8 encoded.?

Json _only_ supports Unicode text. See [http://www.unicode.org/](http://www.unicode.org/)

If you want to send arbitrary binary data, you need to encode it first  
into a textual form, e.g. using base64 encoding.

If you want to sent 8-bit text which is not UTF-8, then you can  
transcode it to UTF-8 (probably the “right thing” in this context), or  
base64 encode it.

---

<div class="post-metadata">

**Author:** ![lyes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ed8c4c/32.png) [@lyes](https://www.ruby-forum.com/u/lyes)\
**Post date:** [January 18, 2009, 8:05am UTC](https://www.ruby-forum.com/t/ruby-json-library-trouble/157752/4 "2009-01-18T08:05:06Z")

</div>

OK, thank you Brian and Izidor,

I will see in that way. But I want to know something more, what is  
exactly  
the role of the KCode Variable?

Than kyou!

---

<div class="post-metadata">

**Author:** ![lyes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ed8c4c/32.png) [@lyes](https://www.ruby-forum.com/u/lyes)\
**Post date:** [January 16, 2009, 12:04pm UTC](https://www.ruby-forum.com/t/ruby-json-library-trouble/157752/5 "2009-01-16T12:04:50Z")

</div>

Sorry I meant [http://www.json.org/](http://www.json.org/) - must engage brain sooner!

---

<div class="post-metadata">

**Author:** ![lyes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ed8c4c/32.png) [@lyes](https://www.ruby-forum.com/u/lyes)\
**Post date:** [January 20, 2009, 9:09am UTC](https://www.ruby-forum.com/t/ruby-json-library-trouble/157752/6 "2009-01-20T09:09:54Z")

</div>

Hello James!

Thank you for the quick answer, I will see whate you gave me

---

<div class="post-metadata">

**Author:** ![lyes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ed8c4c/32.png) [@lyes](https://www.ruby-forum.com/u/lyes)\
**Post date:** [January 18, 2009, 5:40pm UTC](https://www.ruby-forum.com/t/ruby-json-library-trouble/157752/7 "2009-01-18T17:40:57Z")

</div>

On Jan 18, 2009, at 1:03 AM, Lyes A. wrote:

> I will see in that way. But I want to know something more, what is  
> exactly the role of the KCode Variable?

I answer that specific question in this blog post:

[http://blog.grayproductions.net/articles/the\_kcode\_variable\_and\_jcode\_library](http://blog.grayproductions.net/articles/the_kcode_variable_and_jcode_library)

If you want more general character encoding information, you may wish  
to start reading from the beginning of that series:

[http://blog.grayproductions.net/articles/understanding\_m17n](http://blog.grayproductions.net/articles/understanding_m17n)

Hope that helps.

James Edward G. II
