I have a json structure that I'm decoding that looks like this:
person => {
city => "Chicago",
id => 123,
name => "Joe Smith",
pets => {
cats => [
{ age => 6, name => "cat1", type => "siamese", weight => "10 kilos" },
{ age => 10, name => "cat2", type => "siamese", weight => "13 kilos" },
],
dogs => [
{ age => 7, name => "dog1", type => "siamese", weight => "20 kilos" },
{ age => 5, name => "dog2", type => "siamese", weight => "15 kilos" },
],
},
},
}
I'm able to print the city
, id
, name
by doing:
foreach my $listing ($decoded->{person})
{
my $city = $listing->{city};
my $name = $listing->{name};
name - $city - \n";
}
however, I'm unsure of how to print the pets->cats
or pets->dogs
. I'm able to do a dump of them by:
my @pets = $listing->{pets}->{cats};
dump @pets;
but I'm not sure how to access them through the hash structure.
Digging into a big structure is pretty simple, once you know the rules:
{}
[]
->
before the first identifier.->
) are optional.So: *
$data->{person}{name}
returns'Joe Smith'
*$data->{person}->{name}
also returns'Joe Smith'
*$data->{pets}{cats}[0]{age}
returns6
.For way more detail on this topic, see the Perl Data Structures Cookbook (perldoc perldsc)
When you work with big structures like this there are some important things to be aware of. The biggest of these is
autovivification
. Autoviv means that Perl will automatically make data structure elements pop into existence for you to make your life easier. Unfortunately it can also make things difficult.For example, autoviv is great when I do this:
Autoviv magically turns
$data
into a hashref that contains the keyhorse
with an array ref as its value. The array ref gets populated by a hash ref. The final hash ref then gets the key value pair ofcolor => brown
.The problem comes in when you are walking a structure and do deep tests for existence:
Here, autovivification burns you by creating a bunch of junk in data, here's the program output:
$VAR1 = { 'cat' => [ undef, undef, undef, undef, undef, {} ], 'horse' => [ { 'color' => 'brown' } ] };
Now you can guard against this kind of thing by carefully testing each layer of your structure for existence, but it's a huge pain in the butt. Instead, I prefer to use Data::Diver.
$data
is unchanged here.Also, you may have noticed that since
Dive
takes a list of keys or indexes, that means its easy to programatically build up a list of keys/indexes and descend an arbitrary path in your code.Data::Diver can be a real life saver when you have to do a lot of manipulation of big, wonky data structures.
Assuming your
$listing
is a person you have to dereference array and hash refs.and so on for other stuff.
To access them from the structure you can do:
This isn't doing what you think it is.
$listing->{pets}->{cats}
contains a reference to an array. Your new@pets
array ends up contains just one element - the array reference.What you actually need is
This deferences the array reference and gets you the actual array. Notice that I've also dropped the optional second arrow in the expression.
Once you've got the array, each element of it is a hash reference.
Of course, you don't need the intermediate array at all.