Convert TXT file to JSON

I want to convert rather unorganized and unstructured text to JSON format. I want to be able to use the city identifier data. Is there a way to convert this to JSON?

UPDATE: I also found this solution after a while. A very easy way to get the JSON of any tabbed text file.

https://shancarter.imtqy.com/mr-data-converter/

+5
source share
2 answers

You can try using tsv2json , this tool can read tsv file from stdin and write json file to stdout.

It is distributed in the source file, to compile it you need to download the D compiler , and then run dmd tsv2json.d .

If you have a more difficult task, there is another tool called tsv-utils

+4
source

TSV for JSON in nodejs

 var file_name = 'city_list.txt'; var readline = require('readline'); var fs = require('fs'); var lineReader = readline.createInterface({ input: fs.createReadStream(file_name) }); var isHeader = false; var columnNames = []; function parseLine(line) { return line.trim().split('\t') } function createRowObject(values) { var rowObject = {}; columnNames.forEach((value,index) => { rowObject[value] = values[index]; }); return rowObject; } var json = {}; json[file_name] = []; lineReader.on('line', function (line) { if(!isHeader) { columnNames = parseLine(line); isHeader = true; } else { json[file_name].push(createRowObject(parseLine(line))); } }); lineReader.on('close', function () { fs.writeFileSync(file_name + '.json', JSON.stringify(json,null,2)); }); 
0
source

Source: https://habr.com/ru/post/1257566/


All Articles