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

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


in reply to Re^2: 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?

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.


Comment on Re^3: How to use a json string with nested array and nested hash elements?
Select or Download Code
Re^4: How to use a json string with nested array and nested hash elements?
by Anonymous Monk on Feb 04, 2013 at 23:02 UTC
    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://1016942]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2014-08-29 02:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (275 votes), past polls