Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Welcome to the Monastery
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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;


In reply to How to use a json string with nested array and nested hash elements? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (19)
    As of 2014-04-17 15:03 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (451 votes), past polls