http://www.perlmonks.org?node_id=1016921

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I've battled JSON and I've lost this round. :( I don't understand how to extract the nested arrays and hashes. I that it was coded as an ARRAY but when I try to copy the array into a new one it looks like I need to dereference it. I've come to realize I don't understand dereferencing properly. I also having found a clear tutorial at least for my current way of looking at it. :(

I am getting the following json text ($tstJSON) from a service I don't control. The json is a hash that includes an array and I believe there are 2 hashes inside it (as far as I can tell). At the end I need to use a few of the values in the nested array and nested hash in order to calculate how much inventory is left and to select list the best location for refilling the inventory.

Can any kind monk offer some advice on how to properly dereference hashes and arrays from Json?

use strict; use warnings; #use JSON -support_by_pp; # I've tried multiple modules for j +son support. #use JSON qw( decode_json ); # From CPAN use JSON::Parse 'json_to_perl'; # This module is OK use Data::Dumper; # Perl core module use utf8; my $tstJSON = qq< {"deviceid":"iPad","more_info":"G1","supplierid":"13 +58301","prime_name":"&#20132;at1","cost":"1029.32","ntype":"","strarr +ay":[{"inventory_report":{"transactid":1358301537312,"date":"Thu Jan +17 13:35:37 +0800 2013","current_value":126.30,"refill":69.91},"refil +l_report":{"vendorid":1358301537311,"date":"Thu Jan 16 13:35:32 +0800 + 2013","pick_location":[{"pickid":"72b921:3d1939","inv":-25},{"pickid +":"6020a6:7f20e0","inv":67},{"pickid":"5c0c8b:13a351","inv":72},{"pic +kid":"72c941:3d1938","inv":75},{"pickid":"9020f6:7352d1","inv":-23}]} +}]} >; print "\n\n" . $tstJSON ."\n\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n\n"; my $decoded_json = json_to_perl ($tstJSON); print "Ref_type: " . ref( $decoded_json ) . "\n"; print "Find Individual Attributes\n"; print "Device: " .$decoded_json->{deviceid} . "\n"; #OK print "Cost: " . $decoded_json->{cost} . "\n"; #OK print "Details: " . $decoded_json->{more_info} . "\n"; #OK print "Primary Name: " . $decoded_json->{prime_name} . "\n"; #OK print "Next Type: " . $decoded_json->{ntype} . "\n"; #OK print "\nFind Nested Content\n"; # strarray - json array embedded in string. print "Ref_type: " . ref( $decoded_json->{strarray} ) . "\n"; my @jsonarray = $decoded_json->{strarray}; #ref($docoded_json +->{strarry} confirms array. Store in array for easier processing. if (ref(@jsonarray) eq "ARRAY") { #try to print as an array populated from the dereferenc +ed_json print "Print Full String Array:\n"; foreach my $elements (@jsonarray) #Ba +d! Does not work { print "Elements = $elements\n"; } #try to print directly as a dereferenced item print "\nTry Printing Array as Dereferenced Array:\n" +; my @array_ref2 = @{ $decoded_json->{strarray} }; + #Bad! Does not work print "\nStart Array: " . scalar(@jsonarray) . "\ +n\n"; } my $cur_value = $decoded_json->{strarray}->{loc}->{current_value}; # + Bad! Cannot assign value this way my $restock_level = $decoded_json->{strarray}->{loc}->{refill}; # +#Bad! Does not work. Cannot assign value this way my %pickstations => (); # Test code for @sites #my @sites = ( #[ '{"pickid":"72b921:3d1939","inv":-25}'], #[',{"pickid":"6020a6:7f20e0","inv":67},'], #['{"pickid":"5c0c8b:13a351","inv":72}'], #); #foreach my $picklocation (@sites) #trying to get a small s +ample working for the rest of the program. Also not working! foreach my $picklocation ($decoded_json->{strarray}->{loc}->{pick_loca +tion}) { my ($pickid, $inv) = ($picklocation =~ m/pickid":"(\w.+)","inv":(\ +d+)/); print "$pickid, $inv\n"; $pickstations{$pickid} = $inv; } #Determine quantity left before reorder: my $qty_on_stock = $cur_value - $restock_level; print "Current Stock: " . $qty_on_stock . "\n"; print "Quantity till Restock: " . $qty_on_stock . "\n"; #List picksites by inventory values: foreach my $key ( sort { $pickstations{$a}->{inv} cmp $pickstations{$b}->{inv} } keys %pickstations ) { my $value = $pickstations{$key}; print "$pickstations{$key} : $value->{inv} \n"; } print "\n\n"; exit 0;

  • Comment on How to use a json string with nested array and nested hash elements?
  • Download Code

Replies are listed 'Best First'.
Re: How to use a json string with nested array and nested hash elements?
by aitap (Curate) on Feb 04, 2013 at 10:27 UTC
    my @jsonarray = $decoded_json->{strarray};
    It's not an array, but an array reference. You should dereference it: my @jsonarray = @{$decoded_json->{strarray}};. Read perlreftut and perlref for more information on references.
    Sorry if my advice was wrong.
Re: How to use a json string with nested array and nested hash elements?
by Anonymous Monk on Feb 04, 2013 at 11:37 UTC

    Could you tell us what print Dumper(json_to_perl($tstJSON)); says? This way we could help you on the syntax without first installing a JSON module just to see what your hash contains.

      Wow, I didn't know Dumper could be called that way.

      print Dumper(json_to_perl($tstJSON));

      $VAR1 = { 'cost' => '1029.32', 'more_info' => 'G1', 'strarray' => [ { 'refill_report' => { 'pick_location' => [ { 'inv' => '-25', 'pickid' => '72b921:3d1939' }, { 'inv' => '67', 'pickid' => '6020a6:7f20e0' }, { 'inv' => '72', 'pickid' => '5c0c8b:13a351' }, { 'inv' => '75', 'pickid' => '72c941:3d1938' }, { 'inv' => '-23', 'pickid' => '9020f6:7352d1' } ], vendorid' => '1358301537311', 'date' => 'Thu Jan 16 13:35:32 +0800 2013' }, 'inventory_report' => { 'current_value' => '126.30', 'transactid' => '1358301537312', 'date' => 'Thu Jan 17 13:35:37 +0800 2013', 'refill' => '69.91' } } ], 'prime_name' => "\x{4ea4}at1", 'deviceid' => 'iPad', 'ntype' => '', 'supplierid' => '1358301' };

        Well, as aitap mentioned above, @jsonarray = $decoded_json->{strarray} is wrong; it should be $jsonarray = $decoded_json->{strarray}. (It makes no sense to call ref() on an array either.)

        Anyway, since $strarray is an array reference, you access its elements with $strarray->[$index], and if you need the whole array (in the foreach), you need to write @$strarray. (With hashes, the equivalents are $somevar->{$key} and %$somevar, respectively.)

        Your structure is... well, let's tackle the strarray. It is an array of a single element, and that element contains a hash. And that is where you went wrong. I think you missed the arrays altogether. There's one under pick_location and the other one was under strarray. But let's try this in code:

        # let's use an intermediate variable. # it's a good idea when dealing with complex data structures my $strarray = $decoded_json->{strarray}; # enumerate over the first array. for my $str (@$strarray) { # now you have a hash with refill_report # and inventory_report in $str # let's access the pick_location inside the refill_report hash my $refill = $str->{refill_report}; my $pick_aref = $refill->{pick_location}; for my $pick (@$pick_aref) { # now we have one hashref in $pick print $pick->{pickid}, "\n"; } # let's try sorting # $a is an element of @$pick_aref; so it is a hashref my @sorted = sort { $a->{inv} <=> $b->{inv} } @$pick_aref; print Dumper($_) for @sorted; }

        It's not a bad idea to postfix your intermediate variables with _href or _aref (like I did for one variable) just to remember whether you are dealing with a hash reference or an array reference.

        I hope this helps. I spent quite a while pondering on how to access a complex data structure that a module spat out for me, but that was quite a while ago.

Re: How to use a json string with nested array and nested hash elements?
by 7stud (Deacon) on Feb 04, 2013 at 19:22 UTC
    #ref($docoded_json->{strarry} confirms array. Store in array for easi +er processing: my @jsonarray = $decoded_json->{strarray};

    That's not how it works. Look at this code:

    use strict; use warnings; use 5.012; use strict; use warnings; use 5.012; my @arr = ('a', 'b', 'c'); my %hash; $hash{letters} = \@arr; say ref $hash{letters}; --output:-- ARRAY

    The output does not mean that $hash{letters} is an array. The output means that $hash{letters} is a *reference* to an array. In fact, ref() returns an empty string if its argument is not a reference, e.g. an array. So the very fact that ref returns a non-empty string for $hash{letters} means that $hash{letters} is some kind of reference; and the output "ARRAY" tells you what kind of *reference* it is.

    So your ref output means that this line:

    my @jsonarray = $decoded_json->{strarray};

    is equivalent to writing this:

    my @jsonarray = $aref;

    And because the left of the assignment is 'list context', and because the right side is not a list, the right side gets promoted to a one element list, giving you this:

    my @jsonarray = ($aref);

    That succeeds in nesting your reference inside the array @jsonarray. To access the whole array referenced by $aref, you would have to write:

    @{ $jsonarray[0] }

    So originally when you found out that the right side of this assignment:

    my @jsonarray = $decoded_json->{strarray};

    produced ARRAY when you called ref() on it, that meant the right side was a reference to an array, and therefore to get the whole array you needed to dereference the right side:

    my @jsonarray = @{ $decoded_json->{strarray} };