Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi friends! I'm no Perl expert but am trying to parse a file for some information which I would like to use to output some JSON formatted text for consumption by Zabbix. The file I'm parsing contains a derivative of the following text:

#Bob monitoring cd /opt/bob-monitor ### version 2 java -jar libs/BobMonitor.jar -xruntime/bob311_2_acc_list.xml -l/tmp/b +ob311/fail.logs -s/tmp/bob311/results.txt 2>&1 java -jar libs/BobMonitor.jar -xruntime/bob411_2_acc_list.xml -l/tmp/b +ob411/fail.logs -s/tmp/bob411/results.txt 2>&1 ### version 3 java -jar libs/BobMonitor.jar -xruntime/bob312_3_acc_list.xml -l/tmp/b +ob312/fail.logs -s/tmp/bob312/results.txt 2>&1 java -jar libs/BobMonitor.jar -xruntime/bob412_3_acc_list.xml -l/tmp/b +ob412/fail.logs -s/tmp/bob412/results.txt 2>&1 ### version 4 java -jar libs/BobMonitor.jar -xruntime/bob313_4_acc_list.xml -l/tmp/b +ob313/fail.logs -s/tmp/bob313/results.txt 2>&1 java -jar libs/BobMonitor.jar -xruntime/bob413_4_acc_list.xml -l/tmp/b +ob413/fail.logs -s/tmp/bob413/results.txt 2>&1 ....
My Perl script looks as follows:
#!/usr/bin/perl print "{"; print " \"data\":\n\t[\n"; my $filename = '/opt/scripts/fester_monitor.sh'; open(my $fh, '<:encoding(UTF-8)', $filename) or die "Could not open file '$filename' $!"; while (my $row = <$fh>) { chomp($row); for($row) { s/^cd.*//; s/^#.*//; ($festerinstance) = m/(fester(\d+))/; print ",\n"; $return = "\t{ \"{#FESTERINSTANCE}\":\"$festerinstance +\"}" unless /(^\s*$|^\,$)/; print $return; } } print "\t]\n"; print "}\n";
The desired output is something like the following:
{ "data": [ { "{#BOBINSTANCE}":"bob311"}, { "{#BOBINSTANCE}":"bob411"}, { "{#BOBINSTANCE}":"bob411"}, { "{#BOBINSTANCE}":"bob411"}, { "{#BOBINSTANCE}":"bob312"}, { "{#BOBINSTANCE}":"bob412"}, { "{#BOBINSTANCE}":"bob412"}, { "{#BOBINSTANCE}":"bob412"}, { "{#BOBINSTANCE}":"bob313"} .... ] }
Yet, I'm getting this:
{ "data": [ , , , , , , { "{#BOBINSTANCE}":"bob311"}, { "{#BOBINSTANCE}":"bob411"}, { "{#BOBINSTANCE}":"bob411"}, { "{#BOBINSTANCE}":"bob411"}, { "{#BOBINSTANCE}":"bob312"}, { "{#BOBINSTANCE}":"bob412"}, { "{#BOBINSTANCE}":"bob412"}, { "{#BOBINSTANCE}":"bob412"}, { "{#BOBINSTANCE}":"bob313"} .... ] }
The issue is the extra commas at the top of the output- which will not be well-formed JSON. Looking at my code, can any one of you perhaps spot something that I may be doing wrong?


In reply to Problem printing JSON type output by jonathan.vanderwatt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-24 01:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found