2

I have to code below:

#!/usr/intel/bin/perl

use strict;
use warnings;

use JSON::XS;

my $json = '{"Object1":{"Year":"2012","Quarter":"Q3","DataType":"Other 3","Environment":"STEVE","Amount":125},"Object2":{"Year":"2012","Quarter":"Q4","DataType":"Other 2","Environment":"MIKE","Amount":500}}';

my $arrayref = decode_json $json;

for my $array(@$arrayref){
        for my $key (keys(%$array)){
                my $val = $array->{$key};
                print "$key: $val\n";
        }

}

When I compile it, it print me the error "Not an ARRAY reference at generator.pl line 12.".

I want to parse the JSON to an object and get data according to the object with the attributes. How can I do it?

I expect after I parse it, I can use to compare string, print, loop it and so on.

4
  • It's a hashref. You can discover that by ref builtin. Or print the whole thing by one of the number of modules for working with complex data structure, Data::Dumper for example. Commented Nov 28, 2016 at 8:25
  • I don't know what it will return, since there is more than one values and attributes, so I will expect it in array form, so I might print in form of array[0].Year, array[1].Quarter...something like that, or if not, how can I use the value in hash? Commented Nov 28, 2016 at 8:29
  • @zdim how could I use that? Commented Nov 28, 2016 at 8:30
  • The reftells you whether it is HASH or ARRAY (or other) or not a reference -- see docs for ref. The top-level is probably going to be hashref (this is not strictly required), and in your case that's just what you have. Then you iterate over keys %$hashref (what you call $arrayref). Or, use Data::Dumper; print Dumper $reference; to see the whole thing that is in $reference. Commented Nov 28, 2016 at 8:43

2 Answers 2

6

It is not array reference, it is hash reference:

#!/usr/intel/bin/perl

use strict;
use warnings;

use JSON::XS;
use Data::Dumper;

my $json = '{"Object1":{"Year":"2012","Quarter":"Q3","DataType":"Other 3","Environment":"STEVE","Amount":125},"Object2":{"Year":"2012","Quarter":"Q4","DataType":"Other 2","Environment":"MIKE","Amount":500}}';

my $arrayref = decode_json $json;

print Data::Dumper->Dump([$arrayref], [qw(arrayref)]);

And output:

$arrayref = {
              'Object2' => {
                             'Quarter' => 'Q4',
                             'Year' => '2012',
                             'Amount' => 500,
                             'DataType' => 'Other 2',
                             'Environment' => 'MIKE'
                           },
              'Object1' => {
                             'Amount' => 125,
                             'DataType' => 'Other 3',
                             'Year' => '2012',
                             'Environment' => 'STEVE',
                             'Quarter' => 'Q3'
                           }
            };
Sign up to request clarification or add additional context in comments.

Comments

4

There are no arrays there; it is a hash of hashes.

my $hashref = decode_json $json;

for my $object_name (sort keys %$hashref){
        print "In $object_name:\n";
        for my $key (sort keys %{ $hashref->{$object_name} }){
                my $val = $hashref->{$object_name}{$key};
                print "$key: $val\n";
        }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.