Perl array / json


 
Thread Tools Search this Thread
Top Forums Programming Perl array / json
# 1  
Old 06-17-2013
Perl array / json

Hi All

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

Code:
#!/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

Code:
$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
# 2  
Old 06-17-2013
The decoded_json structure is an array of hashes.
So you could treat it as follows.
Code:
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
}

# 3  
Old 06-17-2013
Thanks

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


Code:
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??
# 4  
Old 06-17-2013
You could extract the WARNING lines from the output as follows
Code:
my @lines_out= split/\n/,$test->{output};
for $line (@lines_out){
   print $1 if $line=~/(WARNING:.+)$/;
}

# 5  
Old 06-17-2013
Thanks for all the help

last question ( i hope)


I have the following working

Code:
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
# 6  
Old 06-29-2013
Quote:
Originally Posted by ab52
...
Code:
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
...
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Convert String to an Array using shell scripting in JSON file.

This is the sample json I have pasted here. I want all the IP address strings to be converted into an array. For example "10.38.32.202" has to be converted to everywhere in the JSON. There are multiple IPs in a JSON I am pasting one sample object from the JSON. But the IPs already in an Array... (11 Replies)
Discussion started by: vinshas1
11 Replies

2. Shell Programming and Scripting

UNIX or Perl script to convert JSON to CSV

Is there a Unix or Perl script that converts JSON files to CSV or tab delimited format? We are running AIX 6.1. Thanks in advance! (1 Reply)
Discussion started by: warpmail
1 Replies

3. Programming

Perl Json and Hash

Hi all, i am using the following code to that use curl to that outputs a json, i am stuck it into a hash but i canot figure out how to get just the value i need ( hostname) here is my code use warnings; use strict; use Data::Dumper qw(Dumper); ##use JSON; use JSON::XS; my $curl=... (2 Replies)
Discussion started by: ab52
2 Replies

4. Shell Programming and Scripting

perl code-sequence of json format

Hi All , Below is the perl code. from below code want to confirm one thing that wahtever the sequence of data we are passing through json format which contains 3 tuples of different sequences Eg: ParentID,SystemID,SendingTime,Time,ClientLocation,ClientID, ... (1 Reply)
Discussion started by: aish11
1 Replies

5. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

6. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

7. Shell Programming and Scripting

Array in Perl - Detect several file to be in one array

Hi everyone I have one question about using array in perl. let say I have several log file in one folder.. example test1.log test2.log test3.log and the list goes on.. how to make an array for this file? It suppose to detect log file in the current directory and all the log file will... (3 Replies)
Discussion started by: sayachop
3 Replies

8. Shell Programming and Scripting

perl, put one array into many array when field is equal to sth

Hi Everyone, #!/usr/bin/perl use strict; use warnings; my @test=("a;b;qqq;c;d","a;b;ggg;c;d","a;b;qqq;c;d"); would like to split the @test array into two array: @test1=(("a;b;qqq;c;d","a;b;qqq;c;d"); and @test2=("a;b;ggg;c;d"); means search for 3rd filed. Thanks find the... (0 Replies)
Discussion started by: jimmy_y
0 Replies

9. Shell Programming and Scripting

Perl grep array against array

Hi, Is there any way I can grep an array against another array? Basically here's what I need to do. There will be an array containing some fixed texts and I have to check whether some files contain these lines. Reading the same files over and over again for each different pattern doesnt seem... (1 Reply)
Discussion started by: King Nothing
1 Replies

10. Shell Programming and Scripting

[Perl] Accessing array elements within a sed command in Perl script

I am trying to use a script to replace the header of each file, whose filename are stored within the array $test, using the sed command within a Perl script as follows: $count = 0; while ( $count < $#test ) { `sed -e 's/BIOGRF 321/BIOGRF 332/g' ${test} > 0`; `cat 0 >... (2 Replies)
Discussion started by: userix
2 Replies
Login or Register to Ask a Question