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< {"deviceid":"iPad","more_info":"G1","supplierid":"1358301","prime_name":"交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}]}}]} >; 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 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->{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 sample working for the rest of the program. Also not working! foreach my $picklocation ($decoded_json->{strarray}->{loc}->{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}->{inv} cmp $pickstations{$b}->{inv} } keys %pickstations ) { my $value = $pickstations{$key}; print "$pickstations{$key} : $value->{inv} \n"; } print "\n\n"; exit 0;