{"id":1694,"date":"2012-12-19T01:04:15","date_gmt":"2012-12-19T06:04:15","guid":{"rendered":"http:\/\/www.rexfeng.com\/blog\/?p=1694"},"modified":"2012-12-19T01:04:15","modified_gmt":"2012-12-19T06:04:15","slug":"convert-a-ruby-hash-into-valid-json","status":"publish","type":"post","link":"https:\/\/www.rexfeng.com\/blog\/2012\/12\/convert-a-ruby-hash-into-valid-json\/","title":{"rendered":"Convert a Ruby Hash into valid JSON"},"content":{"rendered":"<p>This post is about how to get a valid JSON (JavaScript Object Notation) snippet from a Ruby Hash.<\/p>\n<p>In the world of Ruby, Ruby loves hashes. It&#8217;s great because it allows you to access values with keys. With [cci]require &#8216;json'[\/cci], you can easily parse a JSON formatted string into a Ruby hash with [cci]JSON.parse(json_string)[\/cci]. You can easily tell hashes by their hash rockets ([cci]=>[\/cci]) and JSON by its usage of colons ([cci]:[\/cci]).<\/p>\n<p>[cc]<br \/>\nrequire &#8216;json&#8217;<br \/>\nanimal_count = {&#8220;tiger&#8221;=>3, &#8220;lion&#8221;=>2, &#8220;dog&#8221;=>2, &#8220;cat&#8221;=>3, &#8220;mouse&#8221;=>1, &#8220;bear&#8221;=>1, &#8220;frog&#8221;=>2, &#8220;fish&#8221;=>1}<br \/>\nanimal_count.class #=> Hash<br \/>\nanimal_count.methods #=> :to_json<br \/>\n[\/cc]<\/p>\n<p>By using [cci]require &#8216;json'[\/cci], you get access to the [cci]to_json[\/cci] method on hashes. Unfortunately, they look like this:<\/p>\n<p>[cc]<br \/>\njson_animals = animal_count.to_json #=> &#8220;{\\&#8221;tiger\\&#8221;:3, \\&#8221;lion\\&#8221;:2, \\&#8221;dog\\&#8221;:2, \\&#8221;cat\\&#8221;:3, \\&#8221;mouse\\&#8221;:1, \\&#8221;bear\\&#8221;:1, \\&#8221;frog\\&#8221;:2, \\&#8221;fish\\&#8221;:1}&#8221;<br \/>\njson_animals.class #=> String<br \/>\n[\/cc]<\/p>\n<p>If you paste the string [cci]&#8221;{\\&#8221;tiger\\&#8221;:3,\\&#8221;lion\\&#8221;:2,\\&#8221;dog\\&#8221;:2,\\&#8221;cat\\&#8221;:3,[\/cci]<br \/>\n[cci]\\&#8221;mouse\\&#8221;:1,\\&#8221;bear\\&#8221;:1,\\&#8221;frog\\&#8221;:2,\\&#8221;fish\\&#8221;:1}&#8221;[\/cci] into <a href=\"http:\/\/jsonlint.com\/\">JSON Lint<\/a>, you&#8217;ll find that it&#8217;s invalid JSON due to a Parse Error.<\/p>\n<p>The backslash character ([cci]\\[\/cci]) is there to escape the quotes for serialization (Ruby object to transportable string) and deserialization (string to Ruby object).<\/p>\n<p>The good news is that the solution in IRB is extremely easy.<\/p>\n<p>[cc]<br \/>\nputs json_animals #=> {&#8220;tiger&#8221;:3,&#8221;lion&#8221;:2,&#8221;dog&#8221;:2,&#8221;cat&#8221;:3,&#8221;mouse&#8221;:1,&#8221;bear&#8221;:1,&#8221;frog&#8221;:2,&#8221;fish&#8221;:1}<br \/>\n[\/cc]<\/p>\n<p>With [cci]puts[\/cci], the new return String passes <a href=\"http:\/\/jsonlint.com\/\">JSON Lint<\/a> as Valid JSON.<\/p>\n<p>Using [cci]puts[\/cci] may seem obvious, but it&#8217;s the seemingly small things like this that make all the difference in the world. One is properly escaped but invalid, while the other is valid, usable JSON.<\/p>\n<p><em>(Thanks to <a href=\"https:\/\/twitter.com\/aviflombaum\">Avi<\/a> for helping me with this @<a href=\"http:\/\/flatironschool.com\/\">Flatironschool<\/a>)<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post is about how to get a valid JSON (JavaScript Object Notation) snippet from a Ruby Hash. In the world of Ruby, Ruby loves hashes. It&#8217;s great because it allows you to access values with keys. With [cci]require &#8216;json'[\/cci], you can easily parse a JSON formatted string into a Ruby hash with [cci]JSON.parse(json_string)[\/cci]. You [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1029],"tags":[1038,1043,1045,332,1042,1044],"class_list":["post-1694","post","type-post","status-publish","format-standard","hentry","category-programming","tag-hash","tag-json","tag-lint","tag-ruby","tag-string","tag-valid"],"_links":{"self":[{"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/posts\/1694","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/comments?post=1694"}],"version-history":[{"count":6,"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/posts\/1694\/revisions"}],"predecessor-version":[{"id":1700,"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/posts\/1694\/revisions\/1700"}],"wp:attachment":[{"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/media?parent=1694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/categories?post=1694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/tags?post=1694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}