I am using a JSON object on a project I am working on. I am doing many updates to this JSON file as it is my primary file to work with. I am using nodejs to handle requests and processes my file.
My JSON data looks something like this:
[
{ field1:123,
field2:"Test",
field3:"New",
field4:"City",
},
{ field1:"123",
field2:"Test2",
field3:"FILE to use",
field4:"Assignment Token",
}
]
Throughout its life cycle my files gets corrupted and a it show a similar pattern every time it gets corrupted. I tried to reduce as many number of call it is possible. The pattern it shows is as follows. Any ideas how to avoid this?
[
{ field1:123,
field2:"Test",
field3:"New",
field4:"City",
},
{ field1:"123",
field2:"Test2",
field3:"FILE to use",
field4:"Assignment Token",
}
]o use",field4:"Assignment Token",}]
The basic pattern I am using to update the file is as such:
app.post('/send-to-waitlist', function(req, res){
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
var reqData = req.body;
console.log(reqData);
var cpso = reqData.CPSO;
try{
var rawData2 = fs.readFileSync('filepath',{encoding: 'utf8'})
var data2 = JSON.parse(rawData2);
var eid = reqData.id;;
for(let d of data2){
if(d.eid==eid){
d.SpecialType="Waiting";
break;
}
}
fs.writeFileSync('filepath', JSON.stringify(data2));
res.json({Message: "Data moved Successfully."});
}catch(e){
console.log(e);
res.json({error:e});
}
})