Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Parse Json using perl

by user786 (Sexton)
on May 21, 2015 at 15:50 UTC ( [id://1127362]=perlquestion: print w/replies, xml ) Need Help??

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

I want to parse the following json output :

My script executes a command and the command provides the json output

im using perl json module.

the output looks like below

[ { "wan" : 0, "lan" : 0, "name" : "HTTP", "wan1" : 0, "lan1" : 0 } ]

From my test script ,when i pass 'wan1' as argument i need to get 0 as the output

use stricts; use warning; use json; # function : sub get_stats { my ($self,$option) = @_; my $cmd = 'cmd'; #command goes here $self->execute($cmd); #$self->output() contains the json output. my $decoded = decode_json($self->output() ); my @out = @{ $decoded->{$self->output()} }; foreach my $line ($self->output( ) ); print $line -> {$option} . "\n" ; }

Function call

$self->{'stats_obj'} = Statistics->new( ip => "ip addr"); my $result = $self->{'stats_obj'}->get_stats('wan'); INFO('Status of the task is :' . $result );
When the script executes it doesn't return anything. any suggestions please

Replies are listed 'Best First'.
Re: Parse Json using perl
by MidLifeXis (Monsignor) on May 21, 2015 at 16:00 UTC

    use json is not the same as use JSON. Case matters to the Perl language.

    --MidLifeXis

Re: Parse Json using perl
by Corion (Patriarch) on May 21, 2015 at 16:01 UTC
    use json;

    There is no such module. Maybe you meant:

    use JSON;

    To proceed further, you should print out the string you have in $self->output() and verify that it matches what you expect. Then you should inspect the output of print Dumper $decoded;

Re: Parse Json using perl
by GrandFather (Saint) on May 22, 2015 at 11:26 UTC

    When I run what you claim is your script I get:

    Can't locate stricts.pm in @INC (you may need to install the stricts m +odule) (@INC contains: C:\Program Files (x86)\ActiveState Komodo IDE +8\lib\support\dbgp\perllib c:/strawberry/perl/site/lib c:/strawberry/ +perl/vendor/lib c:/strawberry/perl/lib .) at noname.pl line 2. BEGIN failed--compilation aborted at noname.pl line 2.

    So either you haven't told us what your output really was, or the code you published isn't the code you ran.

    Perl is the programming world's equivalent of English

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1127362]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2024-04-23 18:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found