Northwood128 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks! I'm new to Perl, and I was given the task of writing a new Nagios plug-in. I have a JSON file with the description of several services we need to check (via curl). Here is a sample of how the JSON file looks
{ "services": [ { "service": "serviceName1", "site": "www.example1.com", "uri": "http://www.url.com/index.html", "option": [ { "name": "option-a", "server": "server-a", "servers": ["server-a1", "server-a2", "server-a3", + "server-a4", "server-a5", "server-b6"] }, { "name": "option-b", "lbvserver": "server-b", "servers": ["server-b1", "server-b2", "server-b3", + "server-b4", "server-b5", "server-b6"] } ] }, { "service": "serviceName2", "site": "www.example2.com", "uri": "http://www.url2.com/index.html", "option": [ { "name": "option-a", "server": "server-a", "servers": ["server-a1", "server-a2", "server-a3", + "server-a4", "server-a5", "server-b6"] }, { "name": "option-b", "lbvserver": "server-b", "servers": ["server-b1", "server-b2", "server-b3", + "server-b4", "server-b5", "server-b6"] } ] } ] }
I managed myself to put that into a variable $config using the JSON module. Now, this is the portion of code that's not working.
my @service = $config->{'services'}; foreach $oneservice (@service){ print $oneservice{'service'}; }
I don't want to actually print "serviceName1", or "serviceName2". I'm trying to get to the "servers" key inside "option". The thing is that I'm missing something. When i run this, i get this
Global symbol "%oneservice" requires explicit package name at ./script +.pl line 102. Execution of ./script.pl aborted due to compilation errors
I've read a lot about iterating on hashes and arrays, but I'm blocked by the complexity of the JSON. Why it tells me about %oneservice if I declared it as $oneservice?

Replies are listed 'Best First'.
Re: Iterate over complex JSON
by LanX (Saint) on Nov 23, 2014 at 02:04 UTC
    nested data structure work with references and you need to dereference them explicitely in Perl.

    please try (untested)

    my $service = $config->{'services'}; foreach $oneservice (@$service){ print $oneservice->{'service'}; }

    see perlref for details about references and perldsc for nested data structures

    HTH! :)

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

      I'll try this. Thanks!! :)
Re: Iterate over complex JSON
by Anonymous Monk on Nov 23, 2014 at 01:41 UTC

    add use diagnostics; to get better error messages; you asked for the free help of strict and warnings but you're not Coping with Scoping yet, see these links and my

    I've read a lot about iterating on hashes and arrays, but I'm blocked by the complexity of the JSON. Why it tells me about %oneservice if I declared it as $oneservice?

    Because you skipped reading perlintro. Because  %foo is accessed as  $foo{bar} but $baz is accessed as  $baz->{bar}. See also References quick reference

      Thanks for the advice!