Created
November 23, 2021 17:40
-
-
Save emmaexe/e97dbe98d4b2a51d72a691f416556d2c to your computer and use it in GitHub Desktop.
Converter for transforming every line of a TXT file into its seperate entry inside a JSON file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const config = { | |
"inputfile":"list.txt", | |
"outputfile":"list.json" | |
} | |
const fs = require('fs') | |
const readline = require('readline'); | |
(async () => { | |
console.log('Started.') | |
const fileStream = fs.createReadStream(config.inputfile); | |
const rl = readline.createInterface({ | |
input: fileStream, | |
crlfDelay: Infinity | |
}); | |
const out = []; | |
rl.on('line', async (line) => { | |
out.push(line) | |
}) | |
rl.on('close', () => { | |
fs.writeFile(config.outputfile, JSON.stringify(out), (err) => { | |
if (err) return console.error(err) | |
else console.log('Completed.') | |
}) | |
}) | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment