I have below JSON input -
{"links":{"self":"/some/path"},"data": [{"type":"some_service","id":"foo","attributes": {"created":true,"active":true,"suspended":false}}, {"type":"some_service","id":"dummy","attributes":{"created":false}}]}
I am using below code -
use strict;
use warnings;
use JSON::XS;
use Data::Dumper;
my $result = decode_json($input);
print Dumper($result) . "\n";
But i am getting below output -
$VAR1 = {
'data' => [
{
'attributes' => {
'active' => bless( do{\(my $o = 1)}, 'JSON::XS::Boolean' ),
'created' => $VAR1->{'data'}[0]{'attributes'}{'active'},
'suspended' => bless( do{\(my $o = 0)}, 'JSON::XS::Boolean' )
},
'id' => 'foo',
'type' => 'some_service'
},
{
'id' => 'dummy',
'attributes' => {
'created' => $VAR1->{'data'}[0]{'attributes'}{'suspended'}
},
'type' => 'some_service'
}
],
'links' => {
'self' => '/some/path'
}
};
Looks like the value in 'created' is $VAR1->{'data'}[0]{'attributes'}{'active'} which does not seems to be accurate and same happens at other places too.
Am i missing somewhere in code or the JSON input has some error? Kindly provide your suggestions.
The JSON decoder is just "mapping/pointing" the values to previous values that have already been parsed. You can see your first
created
points to$VAR1->{'data'}[0]{'attributes'}{'active'},
, the value of which is
true
, just likeactive
should be. You are looking at theData::Dumper
representation of the hash array.If you were to retrieve an element from the Perl variable, you would find that it matches up with your original input:
print $result->{"data"}[0]->{"attributes"}->{"created"}; # prints 1
To print the
Data::Dumper
output without this occurring, simply set this flag in your script:$Data::Dumper::Deepcopy = 1;