http://www.perlmonks.org?node_id=1205018

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

i have this string
$VAR1 = \bless( do{\(my $o = 0)}, 'JSON::PP::Boolean' );
use JSON; $decodejattch = $decoded2->{HasAttachments}; $decodejattch = JSON::decode_json $decodejattch;
i have this error:
sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `echo "malformed JSON string, neither tag, array, object, number, string or atom, at character offset 0 (before "(end of string)") at ./test-mail365.pl line 102.'
malformed JSON string, neither tag, array, object, number, string or atom, at character offset 0 (before "(end of string)") at ./test-mail365.pl line 102.

Replies are listed 'Best First'.
Re: how to decode JSON::PP::Boolean
by tangent (Parson) on Dec 06, 2017 at 16:46 UTC
    The value you are retrieving from $decoded2->{HasAttachments} needs to be treated as a true or false value in your code - it does not need further decoding:
    use JSON; use Data::Dumper; my $json = encode_json( {HasAttachments => \0} ); # \0 is JSON false my $decoded = decode_json( $json ); print Dumper( $decoded->{HasAttachments} ); if ( $decoded->{HasAttachments} ) { print "HasAttachments is true"; } else { print "HasAttachments is false"; } # Prints: $VAR1 = bless( do{\(my $o = 0)}, 'JSON::PP::Boolean' ); HasAttachments is false
Re: how to decode JSON::PP::Boolean
by Corion (Patriarch) on Dec 06, 2017 at 15:58 UTC

    If you see the error sh -c ..., then that means that your file is being run by the shell and not by Perl.

    The code you show does not have 102 lines and does not look as if it is run by your shell at all. Please show a short, self-contained program (and the JSON input) that reproduces the output.

Re: how to decode JSON::PP::Boolean
by Eily (Monsignor) on Dec 06, 2017 at 16:08 UTC

    Corion++. Also your string is perl code, JSON can't do anything with it.