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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, 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.