Discussion (2)2
Log in or sign up to continue

I think it would be easier to write ndjson->json converter. Something like this:

ClassMethod nd2json(file, dir)
{
    set dir = ##class(%File).NormalizeDirectory(dir)
    quit:'##class(%File).Exists(file) $$$ERROR($$$GeneralError, "File " _ file _ " does not exist")
    if '##class(%File).DirectoryExists(dir) {
        do ##class(%File).CreateDirectoryChain(dir)
        
        quit:'##class(%File).DirectoryExists(dir) $$$ERROR($$$GeneralError, "Directory " _ dir _ " does not exist and creation failed")
    }
    
    set stream = ##class(%Stream.FileCharacter).%New()
    do stream.LinkToFile(file)
    while 'stream.AtEnd {
        set json = stream.ReadLine($$$MaxStringLength)
        //set out = ##class(%File).TempFilename("json", dir) // random order
        set out = dir _ $tr($j($i(count), 4), " ", 0) _ ".json" // if the order is important
        set outStream = ##class(%Stream.FileCharacter).%New()
        do outStream.LinkToFile(out)
        do outStream.Write(json)
        do outStream.%Save()
        kill outStream
    }
}