Export to text: Conditional output based on loopend

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?

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 (4.94 KB)
DD.20150202.0322.CET

20150202.JSON.cdubjr.html (4.94 KB)

I Tried that and ran it through a JSON validator. Same issue. Thanks though :slight_smile: 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?

I did not check the JSON code.

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?

DD.20150203.0913.CET

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 ...

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()']'
}