cormanaz has asked for the wisdom of the Perl Monks concerning the following question:

Good Day bros. I have some values in a json file parsed with mod JSON that have values JSON::PP:Boolean. How do I process these to get the true/false value? If I just assign them to another variable they are undef.

Replies are listed 'Best First'.
Re: Getting JSON::PP:Boolean value
by haukex (Archbishop) on May 24, 2017 at 12:54 UTC

    Could you show an SSCCE that reproduces the issue? It works fine for me:

    use Data::Dump; use JSON::PP; my $data = decode_json(q{ { "foo":true, "bar":false } }); dd $data; my ($foo,$bar) = ($data->{foo},$data->{bar}); print "foo=$foo, bar=$bar\n"; __END__ { bar => bless(do{\(my $o = 0)}, "JSON::PP::Boolean"), foo => bless(do{\(my $o = 1)}, "JSON::PP::Boolean"), } foo=1, bar=0

    Update: You mention "mod JSON", but the above code works for me exactly the same no matter if I do use JSON;, use JSON::XS;, or use JSON::PP;. I also added the Data::Dump output to show these are indeed JSON::PP::Boolean objects.

      That works fine for me too. Turns out I was referencing the wrong part of the json. Not firing on all cylinders this morning.
Re: Getting JSON::PP:Boolean value
by hippo (Archbishop) on May 24, 2017 at 13:05 UTC
    If I just assign them to another variable they are undef.

    Here is the SSCCE your post omitted showing that this is not the case.

    use strict; use warnings; use JSON::PP; use Test::More tests => 2; my $in = '{ "z": true, "y": false }'; my $out = decode_json ($in); my $z = $out->{z}; my $y = $out->{y}; is ($z, JSON::PP::true, "z is true" ); is ($y, JSON::PP::false, "y is false" );

      I 100% concur here that OP is mistaken, and must be doing something wrong. I was working heavily with the JSON objectified bool last week writing my GPS software. Here's an example that illustrates what the json->perl conversion looks like, and the resulting output before/after re-assignment:

      use warnings; use strict; use Data::Dumper; use JSON; my $json = '{"true": true, "false": false}'; my $perl = decode_json $json; print Dumper $perl; my $t = $perl->{true}; my $f = $perl->{false}; print "orig true: $perl->{true}, var true: $t\n"; print "orig false: $perl->{false}, var false: $f\n"; __END__ $VAR1 = { 'false' => bless( do{\(my $o = 0)}, 'JSON::PP::Boolean' ), 'true' => bless( do{\(my $o = 1)}, 'JSON::PP::Boolean' ) }; orig true: 1, var true: 1 orig false: 0, var false: 0