Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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


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

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' };


Comment on Re^2: How to use a json string with nested array and nested hash elements?
Download Code
Re^3: How to use a json string with nested array and nested hash elements?
by Anonymous Monk on Feb 04, 2013 at 12:50 UTC

    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.

        They dereference the array copying it to a new variable. This gives easier syntax, but the array is copied which might have performance implications. (They are very slight since the array is only a single element in this case.) Mine should have perhaps been named $strarray_aref since it was still a reference.

        All the variables I was using were references, hence a scalar $variable which get dereferenced with @{ } or %{ } or ->

        One thing I forgot to mention that the outermost loop in my code sample is pretty useless since the array is only a single element. It could be replaced with my $str = $strarray->[0];

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2014-09-16 11:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (10 votes), past polls