{"id":7039,"date":"2014-05-17T00:24:01","date_gmt":"2014-05-17T00:24:01","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/05\/17\/cannot-convert-string-and-fixnum-to-json-in-rails-3-collection-of-common-programming-errors\/"},"modified":"2014-05-17T00:24:01","modified_gmt":"2014-05-17T00:24:01","slug":"cannot-convert-string-and-fixnum-to-json-in-rails-3-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/05\/17\/cannot-convert-string-and-fixnum-to-json-in-rails-3-collection-of-common-programming-errors\/","title":{"rendered":"Cannot Convert String and Fixnum to JSON in Rails 3-Collection of common programming errors"},"content":{"rendered":"<pre><code>    JSON.parse('abc'.to_json) gives this error (in rails):\n\n    JSON::ParserError: 757: unexpected token at '\"abc\"'\n    from \/Users\/mpapper\/.rvm\/gems\/ruby-1.9.2-p290@dfc-site\/gems\/json-1.7.4\/lib\/json\/common.rb:155:in `parse'\n    from \/Users\/mpapper\/.rvm\/gems\/ruby-1.9.2-p290@dfc-site\/gems\/json-1.7.4\/lib\/json\/common.rb:155:in `parse'\n    from (irb):20\n    from \/Users\/mpapper\/.rvm\/gems\/ruby-1.9.2-p290@dfc-site\/gems\/railties-3.1.3\/lib\/rails\/commands\/console.rb:45:in `start'\n    from \/Users\/mpapper\/.rvm\/gems\/ruby-1.9.2-p290@dfc-site\/gems\/railties-3.1.3\/lib\/rails\/commands\/console.rb:8:in `start'\n    from \/Users\/mpapper\/.rvm\/gems\/ruby-1.9.2-p290@dfc-site\/gems\/railties-3.1.3\/lib\/rails\/commands.rb:40:in `'\n    from script\/rails:41:in `require'\n    from script\/rails:41:in `'\n<\/code><\/pre>\n<p>Likewise this fails:<\/p>\n<pre><code> JSON.parse(100.to_json)\n<\/code><\/pre>\n<p>And this too (with a slightly different error):<\/p>\n<pre><code> JSON.parse(1.to_json)\n<\/code><\/pre>\n<p>Why dont I just wrap the outputted json in {} to make it look like real json? Because I&#8217;m writing code that should be able to take any object and serialize it as JSON and pull it back out again.<\/p>\n<p>I think the issue comes from this: JSON looks like a set of encoded key-value pairs all put within {}. But the to_json methods in rails (3.x) don&#8217;t output any {} at all.<\/p>\n<p>Given that, all I really want is an easy way to serialize arbitrary ruby\/rails objects into strings and back again. Already, many objects have a to_json method (String, Fixnum, Array, Hash) but the String and Fixnum versions don&#8217;t produce Json that can be parsed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JSON.parse(&#8216;abc&#8217;.to_json) gives this error (in rails): JSON::ParserError: 757: unexpected token at &#8216;&#8221;abc&#8221;&#8216; from \/Users\/mpapper\/.rvm\/gems\/ruby-1.9.2-p290@dfc-site\/gems\/json-1.7.4\/lib\/json\/common.rb:155:in `parse&#8217; from \/Users\/mpapper\/.rvm\/gems\/ruby-1.9.2-p290@dfc-site\/gems\/json-1.7.4\/lib\/json\/common.rb:155:in `parse&#8217; from (irb):20 from \/Users\/mpapper\/.rvm\/gems\/ruby-1.9.2-p290@dfc-site\/gems\/railties-3.1.3\/lib\/rails\/commands\/console.rb:45:in `start&#8217; from \/Users\/mpapper\/.rvm\/gems\/ruby-1.9.2-p290@dfc-site\/gems\/railties-3.1.3\/lib\/rails\/commands\/console.rb:8:in `start&#8217; from \/Users\/mpapper\/.rvm\/gems\/ruby-1.9.2-p290@dfc-site\/gems\/railties-3.1.3\/lib\/rails\/commands.rb:40:in `&#8217; from script\/rails:41:in `require&#8217; from script\/rails:41:in `&#8217; Likewise this fails: JSON.parse(100.to_json) And this too (with a slightly different error): JSON.parse(1.to_json) Why dont I just wrap the outputted [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7039","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7039","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7039"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7039\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7039"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7039"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7039"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}