Read and Write to JSON-Files

54 views
Skip to first unread message

martink...@gmail.com

unread,
Mar 31, 2018, 11:53:48 AM3/31/18
to nodejs
Hi,

I want to read and write json files. I'm new with nodejs so I don't have much experience.
This is the content of my file and the structure:

{
   "products": {
       "1234": {
           "states": {
               "STATEA": 0,
               "STATEB": 0,
               "STATEC": 0
           },
           "emailContacts": [
               "te...@test.de",
               "te...@test2.de"
           ]
       },
       "1256734": {
           "states": {
               "STATEA": 0,
               "STATEB": 0,
               "STATEC": 0
           },
           "emailContacts": [
              "te...@test.de",
              "te...@test2.de"
           ]
       }
   }
}

I use this file because actually I dont't use a database. 
Now I have a nodejs app which read states from receiving messages, if one state is not normal I want to change the value of the state in the json file ad save the state there. For this I tried this code:

var file = require('./data.json')
console.dir(file.products['1234'].states['STATEA']);
file.products['1234'].states['STATEA']=5;
console.dir(file.products['1234'].states['STATEA']);

It seems to work, but after stopping the nodejs app and looking into the file there is no change in the file.
I tried different things but I didn't find a solution. I think the problem is while running the app I only work on a copy of the file and not on the original file...
I want to store the states in the file for a longer time. Later I want to use a database instead a json file.

Thanks for help! :)


Mark Volkmann

unread,
Mar 31, 2018, 1:37:55 PM3/31/18
to nod...@googlegroups.com
You have to use the built-in fs module to write the new JSON back to the file. You want something like this:

const fs = require('fs');

fs.writeFile('./data.json', JSON.stringify(file), err => {
  if (err) {
    console.error(err);
  } else {
    console.log('It worked!');
  }
});

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+unsubscribe@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/7e03a57f-0384-424f-97a1-9ed911b8a929%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
R. Mark Volkmann
Object Computing, Inc.

martink...@gmail.com

unread,
Mar 31, 2018, 2:56:40 PM3/31/18
to nodejs
I tried it with fs module and it worked.
Thanks! 
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.

To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/7e03a57f-0384-424f-97a1-9ed911b8a929%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages