Skip to content
Advertisement

How can I convert a LOG .txt to .json using JavaScript?

I’d like to know how can i convert a log to .Json using JS. Reading the data in the LOG and converting it to .json, i searched a lot and didnt find anything about it.

Advertisement

Answer

You can simply read from the .txt file then write the contents to a .json file using JSON.stringify to make sure characters such as newlines and double quotes are escaped properly.

For example using Node.js:

const fs = require('fs');
const contents = fs.readFileSync('log.txt', 'utf8');
fs.writeFileSync('log.json', `{ "data": ${JSON.stringify(contents)} }n`);
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement