node.js - Why cant parse JSON from file? -


we have empty json file want write new json objects in file, , array of json objects (and after append new jsons array 'push') write file incoming json object:

fs.writefilesync(tasks, updatedjsonstr, encoding='utf8'); 

where

updatedjsonstr = json.stringify({"iscompleted":false,"task":"dfgdfg","date":"25.06.2015"}); 

so in file see added object. after file our json objects:

tasksjsonobj = json.parse(fs.readfilesync("tasks.json", "utf-8")); 

append new json object string , write again:

updatedjsonstr = json.stringify(tasksjsonobj) + ',' +  json.stringify(newjsontask); 

fs.writefilesync(tasks, updatedjsonstr, encoding='utf8'); see 2 json objects in file. !but when try read file 2 json objects - got error when reading json file ([syntaxerror: unexpected token ,]):

 try{      tasksjsonobj = json.parse(fs.readfilesync(tasks, "utf-8"));      console.log('aaaaa' + json.stringify(tasksjsonobj));      return true;    }catch (err) {      console.log("its not ok!");      console.log(err);      return false;    }

your json formation wrong,

{"iscompleted":false,"task":"dfgdfg","date":"25.06.2015"}, {"newiscompleted":false,"task":"dfgdfg","date":"25.06.2015"} 

this after concat 2 json stringify, invalid.

your json should

[ {"iscompleted":false,"task":"dfgdfg","date":"25.06.2015"}, {"newiscompleted":false,"task":"dfgdfg","date":"25.06.2015"} ] 

for can this

var tasks = []; tasks.push( {"iscompleted":false,"task":"dfgdfg","date":"25.06.2015"}) ); 

so update jsonstr be

updatedjsonstr = json.stringify( tasks ); 

again if want append new json string, can this

tasksjsonobj = json.parse(fs.readfilesync("tasks.json", "utf-8")); tasksjsonobj.push( newjsontask ); 

and stringify , write file.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -