0

We have empty JSON file I want to write new JSON objects in this file, and get from it Array of JSON objects (and after simply append new JSONs to array by 'push') I write to the file incoming JSON object:

fs.writeFileSync(tasks, updatedJsonStr, encoding='utf8');

where

updatedJsonStr = JSON.stringify({"isCompleted":false,"task":"dfgdfg","date":"25.06.2015"});

So in the file we see added object. After we get from the file our JSON objects:

tasksJsonObj = JSON.parse(fs.readFileSync("tasks.json", "utf-8"));

Append new JSON object as string and write it again:

updatedJsonStr = JSON.stringify(tasksJsonObj) + ',' +  JSON.stringify(newJsonTask);

fs.writeFileSync(tasks, updatedJsonStr, encoding='utf8'); So we see 2 JSON objects in the file. !But when I try to read file with 2 JSON objects - I got an error when reading JSON from the 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;
  }
  • "I got an error" - hey, I'm all for suspenseful writing in literature, but here I'd rather you actually told us *what* error you got. Also, adding an appropriate tag for what language/technology you're working with would also be good. At a *guess*, [tag:node.js]? – Damien_The_Unbeliever Jun 25 '15 at 12:59
  • nodeJS, "SyntaxError: Unexpected token ," – Spacemaster Jun 25 '15 at 13:01
  • When you give an error, you have to specify the full error and **the line at which it occured**. Edit your question with the "edit" link to include it. – Kyll Jun 25 '15 at 13:04
  • 1
    I guess you're using JavaScript? right? Anyways there's a little problem with what you are writing. If you want to have multiple JSONs next to each other, you should consider putting them into an array: `updatedJsonStr = []; updatedJsonStr.push(JSON.stringify(tasksJsonObj)); updatedJsonStr.push(JSON.stringify(newJsonTask));` and then write them into file! – Hamed Jun 25 '15 at 13:05
  • @Spacemaster you can use '@' to notify the person (like I did). And also accept my answer below to do everyone a favor as well as yourself :) – Hamed Jun 25 '15 at 13:22

3 Answers3

0

Your JSON formation is wrong,

{"isCompleted":false,"task":"dfgdfg","date":"25.06.2015"}, {"newisCompleted":false,"task":"dfgdfg","date":"25.06.2015"}

this is what you will get after concat two JSON stringify, which is also invalid.

Your JSON should be like this

[ {"isCompleted":false,"task":"dfgdfg","date":"25.06.2015"}, {"newisCompleted":false,"task":"dfgdfg","date":"25.06.2015"} ]

for that you can do something like this

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

so your update jsonstr would be

updatedJsonStr = JSON.stringify( tasks );

again if you want to append a new json string, you can do like this

tasksJsonObj = JSON.parse(fs.readFileSync("tasks.json", "utf-8"));
tasksJsonObj.push( newJsonTask );

and then stringify it and write it back to file.

Sark
  • 4,458
  • 5
  • 26
  • 25
  • So when I try to push new JSON element to main array: var TasksJsonArray = []; function readJsonContent() { try{ TasksJsonArray = JSON.parse(fs.readFileSync(tasks, "utf-8")); ... .... TasksJsonArray.push(JSON.stringify(newJsonTask)); I got an error 'undefined is not a function' on line where try to push .. – Spacemaster Jun 25 '15 at 13:26
  • You can only push object not json string, do like this TasksJsonArray.push( newJsonTask ); – Sark Jun 25 '15 at 13:30
  • Thank you so much .. tryed to know much time how to organize this infernal mechanism .. – Spacemaster Jun 25 '15 at 13:39
0

If you are trying to "extend" the object:

https://lodash.com/docs#assign

deep extend (like jQuery's) for nodeJS

If you are trying to "push" new objects into an array, then you're almost there:

var updatedJsonStr = JSON.stringify([{"isCompleted":false,"task":"dfgdfg","date":"25.06.2015"}]);

fs.writeFileSync(tasks, updatedJsonStr, encoding='utf8');

var tasksJsonArr = JSON.parse(fs.readFileSync("tasks.json", "utf-8"));

tasksJsonArr.push(newJsonTask);
Community
  • 1
  • 1
Tristian
  • 109
  • 1
  • 4
0

There's a little problem with what you are writing. If you want to have multiple JSONs next to each other, you should consider putting them into an array:

updatedJsonStr = [];
updatedJsonStr.push(JSON.stringify(tasksJsonObj));
updatedJsonStr.push(JSON.stringify(newJsonTask)); 

and then write them into file!

Hamed
  • 869
  • 10
  • 26