Perl array / json

Hi All

I have used the below code to print the dumper of a json

#!/usr/bin/perl

use LWP::Simple;                
use JSON qw( decode_json );     
use Data::Dumper;               
use strict;                     
use warnings;

my (%list);                 

my $trendsurl = "http://sensu:4567/events";

my $json = get( $trendsurl );
die "Could not get $trendsurl!" unless defined $json;

my $decoded_json = decode_json( $json );




print Dumper $decoded_json;





which gives me the following


$VAR1 = [                                                                                                                                                                                           
          {                                                                                                                                                                                         
            'client' => 'es1.',                                                                                                                                                            
            'flapping' => bless( do{\(my $o = 0)}, 'JSON::XS::Boolean' ),
            'check' => 'disk_usage',
            'occurrences' => 2160,
            'issued' => 1371466494,
            'status' => 1,
            'handlers' => [
                            'default'
                          ],
            'output' => 'com.dneg.es1.df.boot.df_complex.free OK
WARNING: com.dneg.es1.df.opt-elasticsearch.df_complex.free will run out of space in 10 days at current rate
com.dneg.es1.df.root.df_complex.free OK
'
          },
          {
            'client' => 'graphite1.',
            'flapping' => $VAR1->[0]{'flapping'},
            'check' => 'disk_usage',
            'occurrences' => 7028,
            'issued' => 1371466494,
            'status' => 1,
            'handlers' => [
                            'default'
                          ],
            'output' => 'com.dneg.graphite1.df.boot.df_complex.free OK
com.dneg.graphite1.df.root.df_complex.free OK
WARNING: com.dneg.graphite1.df.user_data.df_complex.free will run out of space in 12 days at current rate
'
          },


I am now stuck in how to use that data, i want to extract the client and the Warning

i.e

client' => 'graphite1.
WARNING: fee will run out of space in 12 days at current rate

any pointers would be great

Thanks
ADam

The decoded_json structure is an array of hashes.
So you could treat it as follows.

for my $test (@{$decoded_json}){ # dereference the structure to an array
  print "$test->{client}\n";
  print "$test->{output}\n"; # using -> to dereference the referenced hash
}

Thanks

but using the output gives loads of extra info, i, e

com.dneg.graphite1.df.boot.df_complex.free OK
com.dneg.graphite1.df.root.df_complex.free OK
WARNING: com.dneg.graphite1.df.user_data.df_complex.free will run out of space in 12 days at current rate

i just want the but after the warning? is this doable??

You could extract the WARNING lines from the output as follows

my @lines_out= split/\n/,$test->{output};
for $line (@lines_out){
   print $1 if $line=~/(WARNING:.+)$/;
}

Thanks for all the help

last question ( i hope)

I have the following working


for my $w (@$decoded_json) {
	
printf "%-12s %-25s",  $w->{client}, $w->{check} if $w->{status} == 1 ;
printf "%-12s %-25s",  $w->{client}, $w->{check} if $w->{status} == 2 ;
	
}

i want to put a title abve the status one called "warning" and "critical" above the status 2. but i want it only about the first one of each section

hope that makes scene

thanks
Again

A

No, it does not make sense.

Your "printf" statements do not have newlines. Hence, all output will be on one long line. All output for status = 1 as well as status = 2 will be on one long line.

Since outputs per status are not separated, there is no way to add a title "above" a particular status. You can, however, put a title either above or below the long output line that your program prints. I believe that's not what you want.

You may want to post some sample input and corresponding sample output so that your problem is clearer to understand.