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;