Cr() vs. lf() in an export procedure

I’m trying to configure a procedure to output a file that requires almost no post-processing before being used.

The code I thought would work is:

local dataset,dataitem,startCode,endCode

arraybuild dataitem,¶,'',
'['+
str(«Check #»)+',"'+
datepattern(«Date»,'YYYY/MM/DD')+'","'+
replace(«Vendor»,'&','&')+'","'+
replace(«Description»,'&','&')+'","'+
pattern(«Check total»,'(#.,##)')+'"],'

dataset={<DATA>}
startCode = '{' + lf() + '"data": [' + lf()
endCode = lf() + ']' + lf() + '}

filesave 'linefeed.json', startCode + replace(dataset,'<DATA>',dataitem) + endCode

After opening linefeed.json, I can see that BBEdit thinks it is a file with Unix linefeeds. However, the character between each record is not a linefeed but some strange “special character” that BBEdit does not recognize. All the lines containing records are run together with that special character between them (shown as • below).

{
"data": [
[112149,"2015/06/03","000003 SO. CAL. ASSOC. OF GOVERNMENTS","ASSESSMENTS FOR FY 2015-16","6,644.00 "],•[96739,"2012/10/10","000009 KAREN KELLEY","EXPENSE REIMB.","19.04 "],•
// etc.
]}

I then tried replacing that first lf() with cr():

local dataset,dataitem,startCode,endCode

arraybuild dataitem,¶,'',
'['+
str(«Check #»)+',"'+
datepattern(«Date»,'YYYY/MM/DD')+'","'+
replace(«Vendor»,'&','&amp;')+'","'+
replace(«Description»,'&','&amp;')+'","'+
pattern(«Check total»,'(#.,##)')+'"],'

dataset={<DATA>}
startCode = '{' + cr() + '"data": [' + lf()
endCode = lf() + ']' + lf() + '}'

filesave 'carriagereturn.json', startCode + replace(dataset,'<DATA>',dataitem) + endCode

Replacing only that first lf() with cr() results in a file that BBEdit thinks is a Legacy Mac file with carriage returns, but there are no unrecognized characters, so the file displays as expected.

{
"data": [
[112149,"2015/06/03","000003 SO. CAL. ASSOC. OF GOVERNMENTS","ASSESSMENTS FOR FY 2015-16","6,644.00 "],
[96739,"2012/10/10","000009 KAREN KELLEY","EXPENSE REIMB.","19.04 "],
// etc.
]
}

Note that in both cases, the leading and trailing linefeeds display correctly:

{
"data": [
// data removed here
]
}

I must be missing something simple, but I can’t figure out what.

Obviously, I can use the cr() version, but it bugs me that I can’t figure out how to construct this procedure correctly.

The ¶ character in your arraybuild statement is specifying a carriage return separator between records. If BBEdit thinks the file is using line feed as the end of line character then it doesn’t recognize carriage return as serving that purpose. Change that line to

arraybuild dataitem,lf(),'',

and I expect you will get the result you want.

Literally the only thing I didn’t try changing. Thanks!