1

I am having a json like below.

$response = {"entries":[{"content":{"eStatus":0,"id":"0","enabled":false,"isOK":false,}}]}
 $responseContent = from_json($response);

when i am using from_json or decode_json within my perl code I get following as the converted json.

                     'content' => {
                                      'id' => '0',
                                      'esrsVeAddress' => '',
                                      'isOK' => bless( do{\(my $o = 0)}, 'JSON::PP::Boolean' ),
                                      'enabled' => $VAR1->{'entries'}[0]{'content'}{'isOK'},
                                      'eStatus' => 0
                                    }

When value of element is not boolean ( true/false ) conversion happens properly but when its boolean then output is corrupted.

If you will see value of element 'enabled' and 'isOK' both are wrong.

Am i using wrong function 'from_json' or 'decode_json' here.

Any suggestions or guidance is appreciated.

This is the way i am trying to use value of element isOK and enabled.

  if (  $isOK = $responseContent->{'entries'}[0]->{'content'}->     {'isOK'} eq "1" ) {
     c4lx_log "value is found to be true and so do some business logic";
 }
 else {
    c4lx_log "value is found to be false and so dont do anything here";
 }

NOTE: Input to fucntion 'from_json' or 'decode_json' is coming from the REST response in format as shown above. I have verified that input is passed correctly and as expected. Its just the conversion that is issue here.

14
  • JSON::PP::Boolean is just an object wrapping 0 (falsy in Perl) and 1 (truthy in Perl). Nothing is being corrupted. Commented Mar 22, 2016 at 18:20
  • Why i am seeing value like this 'isOK' => bless( do{(my $o = 0)}, 'JSON::PP::Boolean' ), 'enabled' => $VAR1->{'entries'}[0]{'content'}{'isOK'}, Commented Mar 22, 2016 at 18:21
  • Correct conversion should be like below 'isOK' => false, 'enabled' => false, Commented Mar 22, 2016 at 18:22
  • Because the string false is truthy in Perl. JSON.pm creates a wrapper for boolean values so they mean the same thing in Perl as they do in the original JSON. See metacpan.org/pod/JSON#true-false Commented Mar 22, 2016 at 18:24
  • Correct I agree to that. But my question is why i see value as 'bless( do{(my $o = 0)}, 'JSON::PP::Boolean' )' instead of false or 1/0. How can i avoid conversion like above. Commented Mar 22, 2016 at 18:27

1 Answer 1

1

If you will see value of element 'enabled' and 'isOK' both are wrong.

No, they are correct. Both ->{enabled} and ->{isOK} are false as in the JSON.

How can i avoid conversion like above

You are actually asking why the module doesn't convert the value (to a string or number). That would cause information to be lost. For example, if the conversion you desire would be performed, encode_json(decode_json($json)) would change the data.

What can be done here to make the value as I am expecting [which is] 'content' => { 'id' => '0', 'esrsVeAddress' => '', 'isOK' => false, 'enabled' => false, 'eStatus' => 0 }

That's impossible using Data::Dumper. Data::Dumper produces valid Perl code, and that's not valid Perl code. However, you can get close to that by using

print(Data::Dumper->Dump(
   [ JSON::false, JSON::true, $responseContent ],
   [qw( $false $true $responseContent )]));

It produces the following output:

$false = bless( do{\(my $o = 0)}, 'JSON::PP::Boolean' );
$true = bless( do{\(my $o = 1)}, 'JSON::PP::Boolean' );
$responseContent = {
                     'entries' => [
                                    {
                                      'content' => {
                                                     'id' => '0',
                                                     'eStatus' => 0,
                                                     'enabled' => $false,
                                                     'isOK' => $false
                                                   }
                                    }
                                  ]
                   };

This is the way i am trying to use value of element isOK and enabled.

It's wrong to expect a boolean to have a specific value. Change

my $isOK;  
if ($isOK = $responseContent->{'entries'}[0]->{'content'}->{'isOK'} eq "1")

to

if ($responseContent->{'entries'}[0]->{'content'}->{'isOK'})

That can be simplified to

if ($responseContent->{entries}[0]{content}{isOK})

If you want to store the result for later, use

my $isOK = $responseContent->{entries}[0]{content}{isOK};
if ($isOK)
Sign up to request clarification or add additional context in comments.

7 Comments

Correct. My question is how can i avoid the conversion here. My code tries to use the value of specific element 'isOK' and 'enabled' to perform some logic. But in current case value does not comes as expected
also if you will see value of enabled becomes as follows : 'enabled' => $VAR1->{'entries'}[0]{'content'}{'isOK'}. It refers isOK, this looks strange to me
All i want is false either remains false or I get 1/0. Some reliable value which I can always rely on to make the logic work
What can be done here to make the value as I am expecting which is either false or any value which i can always reply on
example converted json can be like below : 'content' => { 'id' => '0', 'esrsVeAddress' => '', 'isOK' => false, 'enabled' => false, 'eStatus' => 0 } instead of one shown above.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.