Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Hi Ken,

Finally I made some changes and then also added code to read the json string from a file and parse it. However, my file is going to contain the same kind of json output appended to it continuously over time.

When I try to decode_json the entire file, it doesnt allow, as I believe decode_json looks for the braces to see if it finished. How can I parse multiline json from a file?

Heres the code I have now:

#!/usr/bin/env perl -l use strict; use warnings; use Time::Local; use JSON; use Time::Piece; use Math::Round; use File::Read; use Text::Table; my $DestIP="192.168.127.111"; my $jsonc = read_file ('C:\Documents and Settings\SonicWALL User\My Do +cuments\Scripts\logs\json.txt'); my $data = decode_json join '' => map { chomp; $_ } ($jsonc); #my $data = decode_json $jsonc; my $tb = Text::Table->new( "Date", "Start Time", "End Time", "Time Taken" , "Egress: Conv +eyed", "Egress : Sent" , "Ingress: Conveyed" , "Ingress: Sent"); for (@{$data->{aaData}}) { my $difftime=(@$_[1] - @$_[0]); my $destip=@$_[6]; my @egress=split(',', @$_[8]); my @ingress=split(',', @$_[9]); my $econveyed=$egress[0]/1000000; my $esent=$egress[1]/1000000; my $iconveyed=$ingress[0]/1000; my $isent=$ingress[1]/1000; my $startTime=scalar(localtime(@$_[0])); my $endTime=scalar(localtime(@$_[1])); #Filter only for required traffic and eliminate any traffic less than + 1MB if ( $destip eq $DestIP && $egress[0]>1000000) { #print "@$_"; my $date = $startTime->mdy; my $stime1 = $startTime->hms; my $etime1 =$endTime->hms; $tb->load( [ $date, $stime1 , $etime1 ,convert_time($difftime) , nearest(.01,$e +conveyed) , nearest(.01,$esent), $iconveyed , $isent ], ); } } print $tb;
The file input would be something like:
{"DisplayRecords":"12","Records":"12","sColumns":"startTime,endTime, remoteNode,srcIP,srcPort,destIP,destPort,egress,ingress","aaData":[["1 +375976271" ,"1375976430","LAN","D0:05:FE","172.20.30.2",1093,"172.20.28.2",1330," +1034,348"]]} {"DisplayRecords":"12","Records":"12","sColumns":"startTime,endTime, remoteNode,srcIP,srcPort,destIP,destPort,egress,ingress","aaData":[["1 +375976271" ,"1375976430","LAN","D0:05:FE","172.20.30.2",1093,"172.20.28.2",1330," +1034,348"]]} .... and so on

I couldnt figure this part out. Thanks for your help so far.


In reply to Re^2: Epoch based parser by spikeinc
in thread Epoch based parser by spikeinc

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!
  • 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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (16)
    As of 2015-07-02 16:50 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (44 votes), past polls