Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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


in reply to How to use a json string with nested array and nested hash elements?

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.


Comment on Re: How to use a json string with nested array and nested hash elements?
Download Code
Re^2: How to use a json string with nested array and nested hash elements?
by Anonymous Monk on Feb 04, 2013 at 12:05 UTC
    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.

        Thanks for the code samples! It's exactly what I needed.

        I'm looking at what aitap wrote and what you wrote and I see one difference:

        aitap & 7Stud: my @jsonarray = @{$decoded_json->{strarray}};

        Anonymous Monk: my $strarray = $decoded_json->{strarray};

        I am surprised by the use of a $variable (string) instead of an @variable (array). I expected to need the same variable as aitap and 7Stud mentioned. I will need to restudy the variables and references, it's been nearly 6 years since I last wrote programs.

        Thank all of you for the explanations and examples. You have been very helpful.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1016936]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2015-07-07 03:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (87 votes), past polls