{"id":7,"date":"2012-08-24T18:16:41","date_gmt":"2012-08-24T18:16:41","guid":{"rendered":"http:\/\/beta.robertprice.co.uk\/robblog\/2012\/08\/parsing_json_boolean_values_with_perl-shtml\/"},"modified":"2012-08-24T18:16:41","modified_gmt":"2012-08-24T18:16:41","slug":"parsing_json_boolean_values_with_perl-shtml","status":"publish","type":"post","link":"http:\/\/www.robertprice.co.uk\/robblog\/parsing_json_boolean_values_with_perl-shtml\/","title":{"rendered":"Parsing JSON Boolean Values With Perl"},"content":{"rendered":"<p>A project I&#8217;ve been working on recently has meant me using both the <a href=\"http:\/\/search.cpan.org\/~makamaka\/JSON-2.53\/lib\/JSON.pm\">JSON<\/a> and <a href=\"http:\/\/search.cpan.org\/~mart\/JSON-Streaming-Reader-0.05\/lib\/JSON\/Streaming\/Reader.pm\">JSON::Streaming::Reader<\/a> Perl modules.<\/p>\n<p>The JSON::Streaming::Reader is great for processing large files, and produces results that are compatible with the JSON module. Compatible&#8230; well almost. They both handle booleans in a slightly different way, and that caused me a few problems as I was mainly testing against the JSON module.<\/p>\n<p>In JSON::Streaming::Reader boolean values become references to either 1 or 0.<\/p>\n<p>The JSON module returns a JSON::Boolean (or if using XS &#8211; JSON::XS::Boolean) object that is either JSON::true, or JSON::False. These are overloaded to act as 1 or 0.<\/p>\n<p>If you look at a Data::Dumper output, here are how they compare for a &#8220;true&#8221; value.<\/p>\n<p><strong>JSON::Streaming::Reader<\/strong><\/p>\n<pre class=\"lang:perl decode:true \" >$VAR1 = {\n'OnSale' = 1\n};\n<\/pre>\n<p><strong>JSON<\/strong><\/p>\n<pre class=\"lang:perl decode:true \" >$VAR1 = {\n'OnSale' => bless( do{(my $o = 1)}, 'JSON::XS::Boolean' )\n};\n<\/pre>\n<p>For &#8220;false&#8221; the 1&#8217;s become 0&#8217;s.<\/p>\n<p>When using the JSON module, because the JSON::Boolean object is overloaded, you can test for truthfullness by simply doing&#8230;<\/p>\n<pre class=\"lang:perl decode:true \" >if ($decodedjson->{'OnSale'}) {\n## This is true.\n}\n<\/pre>\n<p>However using JSON::Streaming::Reader this won&#8217;t work, as the reference will always evaluate to true. In this case the value must be dereferenced first before testing.<\/p>\n<pre class=\"lang:perl decode:true \" >if (${$decodedjson->{'OnSale'}}) {\n## This is true.\n}\n<\/pre>\n<p>The good news is that this same block of code will work when also using the JSON module, so in future, when testing decoded boolean values from JSON data, always dereference first!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A project I&#8217;ve been working on recently has meant me using both the JSON and JSON::Streaming::Reader Perl modules. The JSON::Streaming::Reader is great for processing large files, and produces results that are compatible with the JSON module. Compatible&#8230; well almost. They both handle booleans in a slightly different way, and that caused me a few problems &hellip; <a href=\"http:\/\/www.robertprice.co.uk\/robblog\/parsing_json_boolean_values_with_perl-shtml\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Parsing JSON Boolean Values With Perl&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[34,47],"class_list":["post-7","post","type-post","status-publish","format-standard","hentry","category-dev","tag-json","tag-perl"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Parsing JSON Boolean Values With Perl - Robert Price<\/title>\n<meta name=\"description\" content=\"How to parse JSON boolean values using Perl.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.robertprice.co.uk\/robblog\/parsing_json_boolean_values_with_perl-shtml\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Parsing JSON Boolean Values With Perl - Robert Price\" \/>\n<meta property=\"og:description\" content=\"How to parse JSON boolean values using Perl.\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.robertprice.co.uk\/robblog\/parsing_json_boolean_values_with_perl-shtml\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2012-08-24T18:16:41+00:00\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/parsing_json_boolean_values_with_perl-shtml\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/parsing_json_boolean_values_with_perl-shtml\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Parsing JSON Boolean Values With Perl\",\"datePublished\":\"2012-08-24T18:16:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/parsing_json_boolean_values_with_perl-shtml\/\"},\"wordCount\":224,\"keywords\":[\"JSON\",\"Perl\"],\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/parsing_json_boolean_values_with_perl-shtml\/\",\"url\":\"http:\/\/www.robertprice.co.uk\/robblog\/parsing_json_boolean_values_with_perl-shtml\/\",\"name\":\"Parsing JSON Boolean Values With Perl - Robert Price\",\"isPartOf\":{\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/#website\"},\"datePublished\":\"2012-08-24T18:16:41+00:00\",\"author\":{\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"description\":\"How to parse JSON boolean values using Perl.\",\"breadcrumb\":{\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/parsing_json_boolean_values_with_perl-shtml\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.robertprice.co.uk\/robblog\/parsing_json_boolean_values_with_perl-shtml\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/parsing_json_boolean_values_with_perl-shtml\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/www.robertprice.co.uk\/robblog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Parsing JSON Boolean Values With Perl\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/#website\",\"url\":\"http:\/\/www.robertprice.co.uk\/robblog\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Parsing JSON Boolean Values With Perl - Robert Price","description":"How to parse JSON boolean values using Perl.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/www.robertprice.co.uk\/robblog\/parsing_json_boolean_values_with_perl-shtml\/","og_locale":"en_GB","og_type":"article","og_title":"Parsing JSON Boolean Values With Perl - Robert Price","og_description":"How to parse JSON boolean values using Perl.","og_url":"http:\/\/www.robertprice.co.uk\/robblog\/parsing_json_boolean_values_with_perl-shtml\/","og_site_name":"Robert Price","article_published_time":"2012-08-24T18:16:41+00:00","author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/www.robertprice.co.uk\/robblog\/parsing_json_boolean_values_with_perl-shtml\/#article","isPartOf":{"@id":"http:\/\/www.robertprice.co.uk\/robblog\/parsing_json_boolean_values_with_perl-shtml\/"},"author":{"name":"rob","@id":"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Parsing JSON Boolean Values With Perl","datePublished":"2012-08-24T18:16:41+00:00","mainEntityOfPage":{"@id":"http:\/\/www.robertprice.co.uk\/robblog\/parsing_json_boolean_values_with_perl-shtml\/"},"wordCount":224,"keywords":["JSON","Perl"],"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"http:\/\/www.robertprice.co.uk\/robblog\/parsing_json_boolean_values_with_perl-shtml\/","url":"http:\/\/www.robertprice.co.uk\/robblog\/parsing_json_boolean_values_with_perl-shtml\/","name":"Parsing JSON Boolean Values With Perl - Robert Price","isPartOf":{"@id":"http:\/\/www.robertprice.co.uk\/robblog\/#website"},"datePublished":"2012-08-24T18:16:41+00:00","author":{"@id":"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"description":"How to parse JSON boolean values using Perl.","breadcrumb":{"@id":"http:\/\/www.robertprice.co.uk\/robblog\/parsing_json_boolean_values_with_perl-shtml\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.robertprice.co.uk\/robblog\/parsing_json_boolean_values_with_perl-shtml\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/www.robertprice.co.uk\/robblog\/parsing_json_boolean_values_with_perl-shtml\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Parsing JSON Boolean Values With Perl"}]},{"@type":"WebSite","@id":"http:\/\/www.robertprice.co.uk\/robblog\/#website","url":"http:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/7","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=7"}],"version-history":[{"count":0,"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/7\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=7"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=7"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=7"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}