Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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.

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1016942]
[Corion]: Meh. I think I have now the parts down that I want from a simple Javascript frontend for single-page- applications with a aPerl backend. But it seems that all Javascript templating modules either don't support two-way binding (which would be nice) or ...
[Corion]: ... don't support server-side precompilation (which means the client has to compile all templates to Javascript themselves), or are giant frameworks that expect to do everything (which is not what I want)
[Corion]: I feel that there is a talk somewhere in there, either about the structure and parts, or how I used several parts to get a complete whole.
[Corion]: Maybe I can still find something that is compatible with handlebars.js (which has JS precompilation) but allows for two-way data binding (which is great for the UI)

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2017-03-30 07:47 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (355 votes). Check out past polls.