Skip to content

Instantly share code, notes, and snippets.

@petekp
Last active August 29, 2020 01:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petekp/1275bc3ca2ac17632794f9f5267d4f9c to your computer and use it in GitHub Desktop.
Save petekp/1275bc3ca2ac17632794f9f5267d4f9c to your computer and use it in GitHub Desktop.
Convert delimited .txt to .json
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