How to convert a txt file to JSON file and transform the content to JSON
I will walk down the memory lane in this tutorial. I got to think about an assignment I got at one of my first employments back in the days. The assignment was to change a big text file with a lot of entries, into a JSON object. This was a typical assignment for a junior developer, since it would require a lot of copy-paste from one file to another. But I solved this by a simple function in NodeJs instead.
Problem statement
I will show three scenarios in this tutorial that we will use. The first one will be quite simple, we will change a text file that looks like this:
Richard Peter Lisa Emelie Stephen Taylor Nina
Into a JSON object that looks like this:
{ "persons": ["Richard", "Peter", "Lisa", "Emelie", "Stephen", "Taylor", "Nina"] }
Our second scenario will be a little bit harder. We will convert a text file that looks like this:
Richard,23 Peter,12 Lisa,54 Emelie,23 Stephen,11 Taylor,32 Nina,2
To an object like this
{ "persons": [ { "name": "Richard", "age": 23 }, { "name": "Peter", "age": 12 }, { "name": "Lisa", "age": 54 }, { "name": "Emelie", "age": 23 }, { "name": "Stephen", "age": 11 }, { "name": "Taylor", "age": 32 }, { "name": "Nina", "age": 2 } ] }
Our third scenario will transform a text file that looks like this:
cars: tesla volvo ford mercedes motorcycles: yamaha suzuki harley davidson
To a JSON object that looks like this:
{ "cars": ["tesla", "volvo", "ford", "mercedes"], "motorcycles": ["yamaha", "suzuki", "harley davidson"] }
Solution one
Create a file called converter.js. Now, let's solve this step by step.
- We will use "fs" in node for our file reading/writing
const fs = require("fs"); const data = fs.readFileSync("./persons.txt", "utf8");
- Change the data to an array of names
const persons = data.trim().split("\n");
- Create our JSON structure in a const
const json = { persons: [], };
- Add each person to the array and write the new file
persons.forEach((person) => { json.persons.push(person); }); fs.writeFileSync("./persons.json", JSON.stringify(json, null, 2));
That's it. This scenario was quite simple, but imagine doing this manually for 500 names...
Full solution looks like this
const fs = require("fs"); const data = fs.readFileSync("./persons.txt", "utf8"); const persons = data.trim().split("\n"); const json = { persons: [], }; persons.forEach((person) => { json.persons.push(person); }); fs.writeFileSync("./persons.json", JSON.stringify(json, null, 2));
Solution two
For our next solution, we will need to handle the ages as well for each line. The logic we will change is basically in the forEach loop.
persons.forEach((person) => { const parts = person.split(","); json.persons.push({ name: parts[0], age: Number(parts[1]), }); });
So what we are doing here is basically splitting the string by the comma and creating an array of 2 places. So Peter,12 will be ["Peter", "12"]. And then we are creating our JSON object with the two parts, and creating a number from the string.
Full code
const fs = require("fs"); const data = fs.readFileSync("./persons.txt", "utf8"); const persons = data.trim().split("\n"); const json = { persons: [], }; persons.forEach((person) => { const parts = person.split(","); json.persons.push({ name: parts[0], age: Number(parts[1]), }); }); fs.writeFileSync("./persons.json", JSON.stringify(json, null, 2));
Solution three
All right, last solution now guys. First, we change the name of our file and the constants to reflect the problem a little bit better.
const fs = require("fs"); const data = fs.readFileSync("./vehicles.txt", "utf8"); const vehicles = data.trim().split("\n");
Change our json constant as well to only be an empty object.
const json = {};
And in our loop, we need to check for ":" in order to create the new object. We also need to keep track of the "key" of the object to know where to put our next vehicle.
let currentKey = ""; vehicles.forEach((vechicle) => { if (vechicle.endsWith(":")) { currentKey = vechicle.substring(0, vechicle.length - 1); json[currentKey] = []; } else { json[currentKey]?.push(vechicle); } });
- currentKey will hold the current key (cars or motorcycles in this case)
- If the "vehicle" ends with :, then we know it is a new key so we create a new entry in our json object
- If it is not ending with :, we add it to the currentKey in the json object
- vechicle.substring(0, vechicle.length - 1) this line is so we only store cars for example and not : at the end
Full code
const fs = require("fs"); const data = fs.readFileSync("./vehicles.txt", "utf8"); const vehicles = data.trim().split("\n"); const json = {}; let currentKey = ""; vehicles.forEach((vechicle) => { if (vechicle.endsWith(":")) { currentKey = vechicle.substring(0, vechicle.length - 1); json[currentKey] = []; } else { json[currentKey]?.push(vechicle); } }); fs.writeFileSync("./vehicles.json", JSON.stringify(json, null, 2));
Outro
All right, that's it for this tutorial. Today we learned how we can transform a text file to a json file in NodeJs and also how we can programmatically change the plain text to a json object of our choice. This type of scenarios are not that common, but when they do occur, it can be nice to know how to face them.
I hope you found this interesting, and if you have another use case that is harder than the ones I showed here, please reach out and we can see if we can solve it together. Contact info is found here.
All the best,