title: Converting values from for loop to json format
body: <p>Below is a snippet of a for loop where I sort txt file names. I am then trying to save the results in a json format file. However it results in an invalid json format due to the last <code>,</code> inserted in <code>obj</code>. How could i convert to json format the values from the for loop?</p>
  
  <p>script</p>
  
  <pre><code>dir = "myfiles/test/"
  echo "[" &gt;&gt; test.json
  for dir in "${array[@]}"; do
          #reverse the result of comparisons
          file=$(find "$dir" -maxdepth 1 -type f -iname '*.txt' | awk "NR==$i")
          [[ -n $file ]] &amp;&amp; echo "{ \"filepath\" : \"$file\" },"  &gt;&gt; test.json
  done
  echo "]" &gt;&gt; test.json
  </code></pre>
  
  <p>Desired output</p>
  
  <pre><code>[
  { "filepath" : "myfiles/test/sdfsd.txt" },
  { "filepath" : "myfiles/test/piids.txt" },
  { "filepath" : "myfiles/test/saaad.txt" },
  { "filepath" : "myfiles/test/smmnu.txt" }
  ]
  </code></pre>
  
  <p>Current output</p>
  
  <pre><code>[
  { "filepath" : "myfiles/test/sdfsd.txt" },
  { "filepath" : "myfiles/test/piids.txt" },
  { "filepath" : "myfiles/test/saaad.txt" },
  { "filepath" : "myfiles/test/smmnu.txt" },
  ]
  </code></pre>