Last active
August 29, 2020 01:48
-
-
Save petekp/1275bc3ca2ac17632794f9f5267d4f9c to your computer and use it in GitHub Desktop.
Convert delimited .txt to .json
This file contains 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
var fs = require('fs') | |
const jsonifyText = text => { | |
const cleaned = text | |
.toString() | |
.split("\n") | |
.filter(Boolean) | |
.map(item => item.split(/:(.+)/).filter(Boolean)) | |
const structured = { | |
data: cleaned.reduce((acc, cur) => ([ | |
...acc, | |
{ | |
name: cur[0], | |
url: cur[1] | |
} | |
]), []) | |
} | |
return JSON.stringify(structured) | |
} | |
try { | |
const filename = process.argv.slice(2).toString() | |
if (!filename) { | |
throw 'need filename param' | |
} | |
fs.readFile(filename, (err, data) => fs.writeFileSync(filename.replace('.txt', '.json'), jsonifyText(data))) | |
} catch (err) { | |
console.error(err) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment