[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Get info out a JSON file



Actually yes, that doesn't look like JSON code, the => isn't part of
the standard JSON notation.

it would have made your life much easier, not sure about perl itself,
but I think most languages these days have a JSON.load(foobar) or
equivalent that processes the json code, splits into data structures,
hash maps etc.


can't much help with the parsing, but since this file seems pretty
close to JSON, why don't you modify it to be json, and let the already
existing perl code handle the rest.  If you replace the => with a :
and quote the value of $VAR perl's json libs should be able to handle
it.

http://search.cpan.org/~makamaka/JSON-2.16/lib/JSON.pm

if you load it in, then you should be able to just grab the value
$ssh_port = $json['server']['ssh'] (or something along those lines..
not a perl fan )
--
Samir



On Tue, Dec 1, 2009 at 9:50 AM, Rob Bridal <rbridal@anpi.org> wrote:
> This looks like Perl.  Try something like this:
>
> foreach my $key (keys %$VAR1) {
>  print "Service $key uses port $VAR1->{$key}->{port}\n";
> }
>
>
> On 12/1/09 9:35 AM, "Herbie" <technowombat@yahoo.com> wrote:
>
>> I'm attempting to read someone elses JSON files the structure of which is
>> (using Data Dumper):
>>
>> $VAR1 = {
>>            'smtp' => {
>>                        'up_time' => '*',
>>                        'checks' => 2,
>>                        'problem_send_to' => '*',
>>                        'input' => '',
>>                        'marginal_send_to' => '*',
>>                        'port' => 25,
>>                        'marginal' => '*',
>>                        'seconds' => 2,
>>                        'pow' => '*',
>>                        'critical' => '*',
>>                        'vital' => 'no',
>>                        'up_days' => '*'
>>                      },
>>            'ssh' => {
>>                       'up_time' => '*',
>>                       'checks' => 2,
>>                       'problem_send_to' => '*',
>>                       'input' => '',
>>                       'marginal_send_to' => '*',
>>                       'hostname' => 'reginald.some.net',
>>                       'port' => 22,
>>                       'marginal' => 10,
>>                       'seconds' => 2,
>>                       'pow' => '*',
>>                       'critical' => '*',
>>                       'vital' => 'no',
>>                       'up_days' => '*'
>>                     }
>>          };
>>
>> All I'm interested in, is the value of the port number for each service,
>> if I know the service name in advance I can just do something like
>>
>> print $services->{ssh}->{port};
>> print $services->{smtp}->{port};
>>
>> which is well and good, but how do I loop through them if I don't know the
>> service name (ssh, smtp, whatever) in advance? NB the service name doesn't
>> always correspond to what is in /etc/services.
>>
>> h.
>>
>> -
>> To unsubscribe, send email to majordomo@luci.org with
>> "unsubscribe luci-discuss" in the body.
>
>
> -
> To unsubscribe, send email to majordomo@luci.org with
> "unsubscribe luci-discuss" in the body.
>



-- 
--
Samir Faci
*insert title*
`fortune`


-
To unsubscribe, send email to majordomo@luci.org with
"unsubscribe luci-discuss" in the body.