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

Time:05-04

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.

CodePudding user response:

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`);
  • Related