-1

I am trying store data in a .txt file, and then convert it back in to a Javascript list, or array, whatever you call it.

Data in txt file:

[{ firstName: "John", lastName: "Carpenter" },
{ firstName: "Dav", lastName: "Douglas" }]

Then I am trying to make it a list variable:

// my variable
var people = []

// read data from txt file
fs.readFile("./data/data.txt", "utf-8", (err, data) => {
    if (err) { console.log(err) }
    console.log(data, typeof(data));
    people = data
})

Is there a way to convert it from a string to an array?

Caleb Gross
  • 391
  • 3
  • 12
  • 2
    The file content looks almost like JSON, if you can also enclose the keys in `"` you could just use `JSON.parse()` to do the job. – Sirko Jan 03 '22 at 08:41
  • If you're gonna convert to JSON you might as well use `.json` as file extension – Reyno Jan 03 '22 at 08:42
  • Is it slower or faster than a regular .txt file? I want my app to read and write data fast. – Caleb Gross Jan 03 '22 at 08:43
  • So if the Json is going to be fast i will use that, but if not, i'd rather stick with .txt which might be faster. I am mainly going for speed here – Caleb Gross Jan 03 '22 at 08:44
  • Does this answer your question? [How to parse JSON using Node.js?](https://stackoverflow.com/questions/5726729/how-to-parse-json-using-node-js) – admlz635 Jan 03 '22 at 08:44
  • Ok well i guess i can just go with json then... – Caleb Gross Jan 03 '22 at 08:47
  • Ok i just made some simple changes and it worked like a charm! – Caleb Gross Jan 03 '22 at 08:48
  • I will just use Json – Caleb Gross Jan 03 '22 at 08:48
  • `Is it slower or faster than a regular .txt file?` JSON is also text. The file extension is used to give a hint about what type of data the file contains. `.txt` is a generic extension, `.json` a specific one. So no matter how you store your data as text you somehow need to parse it. Your textual representation and the JSON representation are identical from the complexity to parse. – t.niese Jan 03 '22 at 09:12

1 Answers1

1

This is a bad solution, but ONLY if you can't change the format of your input, it might be workable, but be sure you never, ever have user input or foreign input like webrequests in the file, as it will be executed as javascript.

Do not use it unless you know what you are doing!

var data = '[{ firstName: "John", lastName: "Carpenter" },{ firstName: "Dav", lastName: "Douglas" }]'

var people = eval(data)
console.log(people);
body {
background : red;
color: white;
margin-top:0px;
padding-top:0px;
}
h1 {
margin-top:0px;
padding-top:0x;
}
<marquee><blink><h1>EVAL IS EVIL - EVAL IS EVIL - EVAL IS EVIL - EVAL IS EVIL - EVAL IS EVIL</h1></blink></marquee>

WHAT YOU WANT TO USE

Make sure the contents of the file is proper JSON. At this moment it isn't. When generating the file with javascript use JSON.stringify on the variables/arrays you wish to write to the file.

The data will then change to

[{ "firstName": "John", "lastName": "Carpenter" },
{ "firstName": "Dav", "lastName": "Douglas" }]

And then you can use JSON.parse() to decode the data from the file.

var data = '[{ "firstName": "John", "lastName": "Carpenter" },{ "firstName": "Dav", "lastName": "Douglas" }]'

var people = JSON.parse(data);
console.log(people);
Tschallacka
  • 27,901
  • 14
  • 88
  • 133
  • 1
    Thanks! That is exactly what I did. I changed it to JSON instead of what I was originally trying to do. Thanks again! – Caleb Gross Jan 03 '22 at 09:03