At intervals I am collecting data records that should be stored in append-mode to the end of a certain file; I am using json.stringify to render them as text. Later, when I connect to my server, I want to send all those records to the server. Doesn't look like json.parse is capable of handling more than one record; consecutive records are going to make it unhappy. Is there a well-known solution to this problem?
Here's a test program:
fs = require("fs");
const record1 = {
name: "Sarah",
age: 25
};
const record2 = {
name: "Joseph",
age: 34
};
function write() {
var t1 = JSON.stringify(record1);
var t2 = JSON.stringify(record2);
fs.appendFileSync("test.json", t1);
fs.appendFileSync("test.json", t2);
}
function read() {
text = fs.readFileSync("test.json");
var v1 = JSON.parse(text);
console.log(v1);
}
write();
read();
Here's the file test.json:
{"name":"Sarah","age":25}{"name":"Joseph","age":34}
and here's the result:
Process exited with code 1
Uncaught SyntaxError: Unexpected token { in JSON at position 25
So what's the best way to come back and read those two separate records?