<?xml version="1.0" encoding="windows-1252"?>
<node id="1016921" title="How to use a json string with nested array and nested hash elements?" created="2013-02-04 04:53:40" updated="2013-02-04 04:53:40">
<type id="115">
perlquestion</type>
<author id="961">
Anonymous Monk</author>
<data>
<field name="doctext">
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. :(
&lt;p&gt;
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.
&lt;p&gt;
Can any kind monk offer some advice on how to properly dereference hashes and arrays from Json? 
&lt;p&gt;
&lt;readmore&gt;
&lt;code&gt;

use strict;
use warnings;

#use JSON -support_by_pp;          # I've tried multiple modules for json 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&lt; {"deviceid":"iPad","more_info":"G1","supplierid":"1358301","prime_name":"&amp;#20132;at1","cost":"1029.32","ntype":"","strarray":[{"inventory_report":{"transactid":1358301537312,"date":"Thu Jan 17 13:35:37 +0800 2013","current_value":126.30,"refill":69.91},"refill_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},{"pickid":"72c941:3d1938","inv":75},{"pickid":"9020f6:7352d1","inv":-23}]}}]} &gt;;

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-&gt;{deviceid} . "\n";  #OK
print "Cost: " . $decoded_json-&gt;{cost} . "\n";  #OK
print "Details: "  . $decoded_json-&gt;{more_info} . "\n";  #OK
print "Primary Name: "  . $decoded_json-&gt;{prime_name} . "\n";  #OK
print "Next Type: "  . $decoded_json-&gt;{ntype} . "\n";  #OK


print "\nFind Nested Content\n";
# strarray - json array embedded in string.
print "Ref_type: " . ref( $decoded_json-&gt;{strarray} ) . "\n";
       
       my @jsonarray = $decoded_json-&gt;{strarray};   #ref($docoded_json-&gt;{strarry} confirms array.  Store in array for easier processing.
        if (ref(@jsonarray) eq "ARRAY")
        {
 
               #try to print as an array populated from the dereferenced_json
	        print "Print Full String Array:\n";	    
         	foreach my $elements (@jsonarray)                     #Bad! 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-&gt;{strarray} };            #Bad! Does not work
                     print "\nStart Array: " . scalar(@jsonarray) . "\n\n";
         }

my $cur_value = $decoded_json-&gt;{strarray}-&gt;{loc}-&gt;{current_value};   # Bad! Cannot assign value this way
my $restock_level = $decoded_json-&gt;{strarray}-&gt;{loc}-&gt;{refill};     # #Bad! Does not work.  Cannot assign value this way


my %pickstations =&gt; ();


# 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 sample working for the rest of the program.  Also not working!

foreach my $picklocation ($decoded_json-&gt;{strarray}-&gt;{loc}-&gt;{pick_location})
{
   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}-&gt;{inv} cmp $pickstations{$b}-&gt;{inv} }    
    keys %pickstations
    )
{
    my $value = $pickstations{$key};
    print "$pickstations{$key} : $value-&gt;{inv} \n";
}



print "\n\n";

exit 0;

&lt;/code&gt;
&lt;/readmore&gt;
&lt;/p&gt;</field>
<field name="reputation">
3</field>
</data>
</node>
