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?

In reply to Iterate over complex JSON by Northwood128

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.