Perl - Accessing data from decode_json output

348 Views Asked by At

I supply my script with a file of JSON data. I have then decoded the JSON data using decode_json...

open my $fh, '<:encoding(UTF-8)', $file ir die;
    my $jsondata = do {local $/; <$fh> };
    my $data = decode_json($jsondata);

    #print Dumper $data

    #I am trying to write a foreach loop in here to pull particular bits of
    #the information out that I want to display (as detailed further below)

close $fh;

The Dumper output looks like this...

$VAR1 = [
          {
            'DataName' => 'FileOfPetsAcrossTheWorld',
            'Information001' => [
                                  {
                                    'Name' => Steve,
                                    'Sex' => 'Male',
                                    'Age' => 24,
                                    'Animals' => [
                                                 'Dog',
                                                 'Cat',
                                                 'Hamster',
                                                 'Parrot
                                                ],
                                    'Location' => 'London',
                                   },
                                   {
                                    'Name' => Dave,
                                    'Sex' => 'Male',
                                    'Age' => 59,
                                    'Animals' => [
                                                 'Fish',
                                                 'Horse',
                                                 'Budgie',
                                                ],
                                    'Location' => 'Paris',
                                   },
                                   {
                                    'Name' => Sandra,
                                    'Sex' => 'Female',
                                    'Age' => 44,
                                    'Animals' => [
                                                 'Snake',
                                                 'Crocodile',
                                                 'Flamingo',
                                                ],
                                    'Location' => 'Syndey',
                                   }
                                 ]
           }
        ];

I am trying to retrieve output from this data structure using a foreach look so that I can print the output...

Dataname: FileOfPetsAcrossTheWorld
Name: Steve
Animals: Dog, Cat, Parrot, Hamster
Location: London

Name: Dave
Animals: Fish, Horse, Budgie
Location: Paris

Name: Sandra
Animals: Snake, Crocodile, Flamingo
Location: Sydey

I have tried various different foreach loops and hash referencing code snippets from online sources (and some that I have used and had working previously) to iterate through and pull data from hashes etc, but I cannot seem to get it working in this case. Amongst other errors, I receive errors such as 'Not a HASH reference at...'.

What is the correct method I should be using to pull this information out of this type of data structure?

1

There are 1 best solutions below

0
choroba On
for my $hash (@$data) {
    say "Dataname: $hash->{DataName}";
    for my $info (@{ $hash->{Information001} }) {
        say "Name: $info->{Name}";
        say 'Animals: ', join ', ', @{ $info->{Animals} };
        say "Location: $info->{Location}";
        say "";
    }
}

The order of Animals is different for Steve. Sydney is spelled "Sydey".