# Ruby 1.9.1 built-in JSON troubles

**URL:** <https://www.ruby-forum.com/t/ruby-1-9-1-built-in-json-troubles/184693>\
**Category:** Ruby\
**Created:** [March 6, 2010, 9:55pm UTC](https://www.ruby-forum.com/t/ruby-1-9-1-built-in-json-troubles/184693 "2010-03-06T21:55:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aaron\_DSGifford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e8cbc/32.png) [@Aaron\_DSGifford](https://www.ruby-forum.com/u/Aaron_DSGifford)\
**Post date:** [March 6, 2010, 9:55pm UTC](https://www.ruby-forum.com/t/ruby-1-9-1-built-in-json-troubles/184693/1 "2010-03-06T21:55:36Z")

</div>

I’m puzzled. On a box running Ruby 1.9.1 I try this:

user@host:/path$ irb  
irb(main):001:0\> require ‘json’  
=\> true  
irb(main):002:0\> JSON.parse(JSON.generate(’\’))  
JSON::ParserError: 598: unexpected token at ‘"\"’  
from /usr/local/lib/ruby/1.9/json/common.rb:122:in `parse' from /usr/local/lib/ruby/1.9/json/common.rb:122:in`parse’  
from (irb):2  
from /usr/local/bin/irb:12:in `’  
irb(main):003:0\> RUBY\_VERSION  
=\> “1.9.1”  
irb(main):004:0\> ^D

This is the JSON module that came with 1.9–I don’t have a JSON gem  
installed.

Any ideas where to go looking? A little web searching showed me  
various similar problems, but most were under 1.8.

Looking for a clue,  
Aaron out.

---

<div class="post-metadata">

**Author:** ![Aaron\_DSGifford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e8cbc/32.png) [@Aaron\_DSGifford](https://www.ruby-forum.com/u/Aaron_DSGifford)\
**Post date:** [March 8, 2010, 1:39pm UTC](https://www.ruby-forum.com/t/ruby-1-9-1-built-in-json-troubles/184693/2 "2010-03-08T13:39:50Z")

</div>

Aaron D. Gifford wrote:

> irb(main):002:0\> JSON.parse(JSON.generate(’\’))  
> JSON::ParserError: 598: unexpected token at ‘"\"’

This is simply because JSON.parse only parses JSON _objects_ and  
_arrays_, not strings or numbers.

> > JSON.parse(’{“foo”:“bar”}’)  
> > =\> {“foo”=\>“bar”}
> 
> > JSON.parse(’[“foo”,“bar”]’)  
> > =\> [“foo”, “bar”]
> 
> > JSON.parse(’“bar”’)  
> > JSON::ParserError: 574: unexpected token at ‘“bar”’

I use the following workaround:

> > json = JSON.generate(’\’)  
> > =\> “”\\""
> 
> > JSON.parse("[" + json + “]”).first  
> > =\> “\”

HTH,

Brian.

---

<div class="post-metadata">

**Author:** ![Aaron\_DSGifford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e8cbc/32.png) [@Aaron\_DSGifford](https://www.ruby-forum.com/u/Aaron_DSGifford)\
**Post date:** [March 8, 2010, 6:46pm UTC](https://www.ruby-forum.com/t/ruby-1-9-1-built-in-json-troubles/184693/3 "2010-03-08T18:46:51Z")

</div>

On Mon, Mar 8, 2010 at 5:39 AM, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> =\> [“foo”, “bar”]  
> HTH,
> 
> Brian.

Thanks for the note, Brian.

The JSON documentation does NOT make that clear.

And that limitation is a terrible limitation. Ugly!

I ended up rolling my own pure Ruby JSON parser that doesn’t have that  
work-around requirement. It looks like I could have avoided that with  
your work-around (even though it’s sad that the JSON library requires  
such an ugly kludge).

Aaron out.

---

<div class="post-metadata">

**Author:** ![Aaron\_DSGifford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e8cbc/32.png) [@Aaron\_DSGifford](https://www.ruby-forum.com/u/Aaron_DSGifford)\
**Post date:** [March 8, 2010, 7:02pm UTC](https://www.ruby-forum.com/t/ruby-1-9-1-built-in-json-troubles/184693/4 "2010-03-08T19:02:40Z")

</div>

As per Brian’s suggestion, I used his workaround this way:  
(IMHO, JSON ought to handle this automagically.)

require ‘json’  
def json\_parse\_kludge(data)  
return JSON.parse(’[’ + data + ‘]’)[0] if data.is\_a?(String) &&  
data[0,1] == ‘"’  
JSON.parse(data)  
end

Fixes my issue: json\_parse\_kludge(JSON.generate(’\’))

irb(main):001:0\> require ‘json’  
=\> true  
irb(main):002:0\> def json\_parse\_kludge(data)  
irb(main):003:1\> return JSON.parse(’[’ + data + ‘]’)[0] if  
data.is\_a?(String) && data[0,1] == ‘"’  
irb(main):004:1\> JSON.parse(data)  
irb(main):005:1\> end  
=\> nil  
irb(main):006:0\> json\_parse\_kludge(JSON.generate(’//’))  
=\> “//”  
irb(main):007:0\>

Thanks again, Brian!

Aaron out.

---

<div class="post-metadata">

**Author:** ![Aaron\_DSGifford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e8cbc/32.png) [@Aaron\_DSGifford](https://www.ruby-forum.com/u/Aaron_DSGifford)\
**Post date:** [March 8, 2010, 8:19pm UTC](https://www.ruby-forum.com/t/ruby-1-9-1-built-in-json-troubles/184693/5 "2010-03-08T20:19:44Z")

</div>

Aaron D. Gifford wrote:

> Thanks for the note, Brian.
> 
> The JSON documentation does NOT make that clear.

I could understand if it only allowed an object at the top level (which  
is what CouchDB requires), but I agree it doesn’t make sense to allow  
two types of values but not the other types.

Your kludge is a bit messy, it won’t parse ’ “foo”’ for example (with a  
leading space). If you are afraid of building an extra string, then how  
about:

def jparse(str)  
return JSON.parse(str) if str =~ /\A\s\*[{[]/  
JSON.parse("[#{str}]")[0]  
end

---

<div class="post-metadata">

**Author:** ![Aaron\_DSGifford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e8cbc/32.png) [@Aaron\_DSGifford](https://www.ruby-forum.com/u/Aaron_DSGifford)\
**Post date:** [March 9, 2010, 1:41am UTC](https://www.ruby-forum.com/t/ruby-1-9-1-built-in-json-troubles/184693/6 "2010-03-09T01:41:20Z")

</div>

On Mon, Mar 8, 2010 at 12:19 PM, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> … If you are afraid of building an extra string, then how  
> about:
> 
> def jparse(str)  
> return JSON.parse(str) if str =~ /\A\s\*[{[]/  
> JSON.parse(“[#{str}]”)[0]  
> end

Definitely more robust, since that will handle numbers too. Thanks

Aaron out.
