0

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
yonetpkbji
  • 1,019
  • 2
  • 21
  • 35
  • 2
    Can you post what code you have so it can be fixed? This is not a code-writing service, and showing what you've tried will help focus the answer on what fix is required. – Tanktalus Jun 21 '19 at 15:57
  • Single quote missing after `Parrot`, missing quotes around names. – choroba Jun 21 '19 at 16:11
  • 1
    "_have tried various ... code snippets from online sources_" -- use [perlreftut](https://perldoc.perl.org/perlreftut.html) and [perldsc](https://perldoc.perl.org/perldsc.html) instead – zdim Jun 21 '19 at 16:14
  • 2
    Don't use the `:encoding(UTF-8)` layer when reading the file -- use `:raw` if anything, or `read_binary` from [File::Slurper](https://metacpan.org/pod/File::Slurper) for example. `decode_json` already decodes from UTF-8 as JSON files are expected to be in. – Grinnz Jun 21 '19 at 16:43
  • Or use `JSON::XS->new->decode($jsondata)` if it's already been converted from UTF-8 to perl's native encoding. – Shawn Jun 21 '19 at 22:40

1 Answers1

1
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".

choroba
  • 231,213
  • 25
  • 204
  • 289