Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

How do I make the JSON module convert true/false to 1/0 instead of a blessed Boolean object?

by Cody Fendant (Hermit)
on Jan 03, 2022 at 03:13 UTC ( #11140088=perlquestion: print w/replies, xml ) Need Help??

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

Converting some simple JSON:

{ "adult": false, "backdrop_path": "/tdo5GwYAsdRkYTTMpXM9aZu8DlH.jpg", "genre_ids": [ 53, 80, 28 ], "id": 11258, "original_language": "en", "original_title": "Hard Rain", "overview": "Get swept up in the action as an armored car driver ( +Christian Slater) tries to elude a gang of thieves (led by Morgan Fre +eman) while a flood ravages the countryside. Hard Rain is \"a wild, t +hrilling, chilling action ride\" filled with close calls, uncertain l +oyalties and heart-stopping heroics.", "popularity": 12.186, "poster_path": "/hhG5ppaEQIV83GbUVfPlBMDFvVu.jpg", "release_date": "1998-01-16", "title": "Hard Rain", "video": false, "vote_average": 5.8, "vote_count": 519 }

and the false value comes out as an object  bless( do{\(my $o = 0)}, 'JSON::PP::Boolean' ) when converted to Perl data:

$VAR1 = { 'original_language' => 'en', 'vote_count' => 519, 'overview' => 'Get swept up in the action as an armored car +driver (Christian Slater) tries to elude a gang of thieves (led by Mo +rgan Freeman) while a flood ravages the countryside. Hard Rain is "a +wild, thrilling, chilling action ride" filled with close calls, uncer +tain loyalties and heart-stopping heroics.', 'id' => 11258, 'title' => 'Hard Rain', 'video' => bless( do{\(my $o = 0)}, 'JSON::PP::Boolean' ), 'backdrop_path' => '/tdo5GwYAsdRkYTTMpXM9aZu8DlH.jpg', 'release_date' => '1998-01-16', 'vote_average' => '5.8', 'adult' => $VAR1->{'video'}, 'poster_path' => '/hhG5ppaEQIV83GbUVfPlBMDFvVu.jpg', 'original_title' => 'Hard Rain', 'popularity' => '12.186', 'genre_ids' => [ 53, 80, 28 ] };

Which is a little unsightly and hard to understand as a human. I mean I know it can be tested as a true or false value, but it's much harder to read.

What's worse is, the JSON boolean for 'adult' is now  $VAR1->{'video'} which means I have to refer to the previous object to even figure out what it is. Not a problem in a small object like this but in a bigger one it will be very frustrating.

Is there some way these things can be converted to simple 1 and 0 values? I swear I've read the documentation multiple times and can't see it.

Or can I get Data::Dumper to do it for me?

  • Comment on How do I make the JSON module convert true/false to 1/0 instead of a blessed Boolean object?
  • Select or Download Code

Replies are listed 'Best First'.
Re: How do I make the JSON module convert true/false to 1/0 instead of a blessed Boolean object?
by ikegami (Patriarch) on Jan 03, 2022 at 06:49 UTC

    Use boolean_values

      Thanks, that looks like what I should be doing all right.

      But doing this:

      use JSON; my $j = JSON->new(); $j->boolean_values( 1, 0 );

      Gets me Can't locate object method "boolean_values" via package "JSON"

      What am I doing wrong?

        Hello Cody Fendant

        grepping CPAN it seems the method ships with JSON::PP and you can read about it

        # Note that JSON version 2.0 and above will automatically use # JSON::XS or JSON::PP, so you should be able to just: use JSON;

        This probably means you have also JSON::XS installed and it is loaded first

        L*

        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

        works fine for me, JSON v4.03, JSON::PP v4.06

        Are you using version 4+?

Re: How do I make the JSON module convert true/false to 1/0 instead of a blessed Boolean object?
by Anonymous Monk on Jan 03, 2022 at 03:41 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11140088]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (1)
As of 2022-07-02 07:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (102 votes). Check out past polls.

    Notices?