# Export to text: Conditional output based on loopend

**URL:** https://community.mp3tag.de/t/export-to-text-conditional-output-based-on-loopend/16555
**Category:** General Discussion
**Created:** [February 1, 2015, 7:44pm UTC](https://community.mp3tag.de/t/export-to-text-conditional-output-based-on-loopend/16555 "2015-02-01T19:44:30Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![cdubjr](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/c/aeb1de/32.png) [@cdubjr](https://community.mp3tag.de/u/cdubjr)
#### Post date: [February 1, 2015, 7:44pm UTC](https://community.mp3tag.de/t/export-to-text-conditional-output-based-on-loopend/16555/1 "2015-02-01T19:44:30Z")

</div>

Hi, I am attempting to output folders in my library to Json format so that I can stuff album information into Mongo DB. The following outputs invalid Json.

{  
"generalInformation":

```
{
"album": "%album%",
"albumArtist": "%albumartist%",
"genre": "%genre%",
"publisher": "%publisher%",
"copyright": "%copyright%",
"year": "%year%"
},

```

"encodingSettings":

```
{
"averageBitRate": "$loop(%_filename_ext%)$puts(Vbitrate,$add($get(Vbitrate),%_bitrate%))$puts(cnt1,$add($get(cnt1),1))$loopend()$div($get(Vbitrate),$get(cnt1)) kbps %_vbr%",
"averageSampleRate": "%_samplerate% khz",
"mode": "%_mode%",
"codec": "%_codec%",
"tagFormat": "%_tag%",
"originalCodecType": "%mediatype%"
},

```

"totals":

```
{
"totalSize": "%_total_size%",
"totalTracks": "%_total_files%",
"totalLength": "%_total_time%"
},

```

"tracks": '[' $loop(%\_filename\_ext%)

```
{ 
"trackNumber": "$num(%track%,2)",
"title": "%title%",
"artists": "%artist%",
"composers": "%composer%",
"length": "%_length%",
"bitRate": "%_bitrate%kbps %_vbr%",
"fileName": "%_filename_ext%",
"fileSize": "%_file_size%"

```

 },

$loopend()']'  
}

The issue is the LAST comma. What I need is a way to determine if the script is outputting information on the LAST FILE in the folder and if it is skip the last comma. Does anyone have any guidance here? What I am wondering is if I can conditionally output the comma based on whether or not the script is processing the last file in a folder or not. Is that possible?

---

<div class="post-metadata">

### Author: ![DetlevD](https://community.mp3tag.de/user_avatar/community.mp3tag.de/detlevd/32/123_2.png) [@DetlevD](https://community.mp3tag.de/u/DetlevD)
#### Post date: [February 2, 2015, 1:22am UTC](https://community.mp3tag.de/t/export-to-text-conditional-output-based-on-loopend/16555/2 "2015-02-02T01:22:26Z")

</div>

> [@cdubjr](#):
>
> ... The following outputs invalid Json. ... Does anyone have any guidance here?

See the colored output text from the attached html file.  
There is no change in the JSON script.  
There is only modified the Mp3tag export script text to get syntactically correct text output from the Mp3tag export scripting language.  
[20150202.JSON.cdubjr.html](https://community.mp3tag.de/uploads/default/original/2X/5/51920905be1bf72e75dabcbe050a683a707c1754.html) (4.94 KB)  
DD.20150202.0322.CET

[20150202.JSON.cdubjr.html](https://community.mp3tag.de/uploads/default/original/2X/5/51920905be1bf72e75dabcbe050a683a707c1754.html) (4.94 KB)

---

<div class="post-metadata">

### Author: ![cdubjr](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/c/aeb1de/32.png) [@cdubjr](https://community.mp3tag.de/u/cdubjr)
#### Post date: [February 2, 2015, 2:40am UTC](https://community.mp3tag.de/t/export-to-text-conditional-output-based-on-loopend/16555/3 "2015-02-02T02:40:28Z")

</div>

> [@DetlevD](#):
>
> See the colored output text from the attached html file.  
> There is no change in the JSON script.  
> There is only modified the Mp3tag export script text to get syntactically correct text output from the Mp3tag export scripting language.  
> [20150202.JSON.cdubjr.html](https://community.mp3tag.de/uploads/default/original/2X/5/51920905be1bf72e75dabcbe050a683a707c1754.html) (4.94 KB)  
> DD.20150202.0322.CET

I Tried that and ran it through a JSON validator. Same issue. Thanks though 🙂 What I am wondering is if I can conditionally output the comma based on whether or not the script is processing the last file in a folder or not. Is that possible?

---

<div class="post-metadata">

### Author: ![DetlevD](https://community.mp3tag.de/user_avatar/community.mp3tag.de/detlevd/32/123_2.png) [@DetlevD](https://community.mp3tag.de/u/DetlevD)
#### Post date: [February 2, 2015, 7:28am UTC](https://community.mp3tag.de/t/export-to-text-conditional-output-based-on-loopend/16555/4 "2015-02-02T07:28:30Z")

</div>

> [@cdubjr](#):
>
> I Tried that and ran it through a JSON validator. Same issue. ...

I did not check the JSON code.

> [@cdubjr](#):
>
> ... What I am wondering is if I can conditionally output the comma based on whether or not the script is processing the last file in a folder or not. Is that possible?

It seems that you describe a problem with a comma separated list, and with a trailing comma at end of the list.  
There might be more than one possible solutions.  
One is, do not write a trailing comma after each array object, but write a leading comma before each following array object, starting from object #2.  
For the very first array object, suppress the leading comma.

(... this seems to be semantically correct, because the separator comma is indeed the connector between the previous and the following item, and the separator comma is indeed a property of the following item, and a leading separator comma is not defined for the first item in the list).

Example:

**01: $filename($getEnv('USERPROFILE')'\Desktop\Export.Whatever.txt',UTF-8)** 

**02: $loop(%\_filename\_ext%)$ifgreater(%\_counter%,1,',',)'{'  
03: '"trackNumber":"'$num(%TRACK%,2)'",'  
04: '"title":"'%TITLE%'",'  
05: '"artists":"'%ARTIST%'",'  
06: '"composers":"'%COMPOSER%'",'  
07: '"length":"'%\_length%'",'  
08: '"bitRate":"'[%\_bitrate%'kbps'[' '%\_vbr%]]'",'  
09: '"fileName":"'%\_filename\_ext%'",'  
10: '"fileSize":"'%\_file\_size%'"'  
11: '}'$loopend()**

 

DD.20150202.0928.CET, DD.20150202.1211.CET

 

See also ... the other way round ... a slightly other problem ...  
"Determine the last loop"  
... which needs a little bit more coding ... my proposal from there ...

 

[Determine the last loop inside loop?](https://community.mp3tag.de/t/7231/5)

 

DD.20150203.0913.CET

---

<div class="post-metadata">

### Author: ![ohrenkino](https://community.mp3tag.de/user_avatar/community.mp3tag.de/ohrenkino/32/4843_2.png) [@ohrenkino](https://community.mp3tag.de/u/ohrenkino)
#### Post date: [February 2, 2015, 8:58am UTC](https://community.mp3tag.de/t/export-to-text-conditional-output-based-on-loopend/16555/5 "2015-02-02T08:58:17Z")

</div>

Just a very wild idea: is it possible (yes, probably) to send a $CHAR(8) (=Backspace) to the text file so that it deletes the last character? This would need to be tested ...

---

<div class="post-metadata">

### Author: ![cdubjr](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/c/aeb1de/32.png) [@cdubjr](https://community.mp3tag.de/u/cdubjr)
#### Post date: [February 3, 2015, 12:51am UTC](https://community.mp3tag.de/t/export-to-text-conditional-output-based-on-loopend/16555/6 "2015-02-03T00:51:05Z")

</div>

Fixed it... Found a post to a similar question in the discussion board. Here's the fix. I have highlighted the changes..

$filename(json,utf-8)$loop(%\_path%)$puts(CountMax,%\_counter%)$loopend(){  
"generalInformation":

```
{
"albumArtist": "%albumartist%",
"album": "%album%",
"genre": "%genre%",
"year": "%year%", 
"publisher": "%publisher%",
"copyright": "%copyright%"
},

```

"encodingSettings":

```
{
"averageBitRate": "$loop(%_filename_ext%)$puts(Vbitrate,$add($get(Vbitrate),%_bitrate%))$puts(cnt1,$add($get(cnt1),1))$loopend()$div($get(Vbitrate),$get(cnt1)) kbps %_vbr%",
"averageSampleRate": "%_samplerate% khz",
"mode": "%_mode%",
"codec": "%_codec%",
"tagFormat": "%_tag%",
"originalCodecType": "%mediatype%"
},

```

"totals":

```
{
"totalSize": "%_total_size%",
"totalTracks": "%_total_files%",
"totalLength": "%_total_time%"
},

```

"tracks": '[' $loop(%\_filename\_ext%)

```
{ 
"trackNumber": "$num(%track%,2)",
"title": "%title%",
"artists": "%artist%",
"composers": "%composer%",
"length": "%_length%",
"bitRate": "%_bitrate%kbps %_vbr%",
"fileName": "%_filename_ext%",
"fileSize": "%_file_size%"
}<!--coloro:#8B0000--><span style="color:#8B0000"><!--/coloro-->$if($neql($get(CountMax),%_counter%),',',)<!--colorc--></span><!--/colorc-->

```

$loopend()']'  
}
