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!');
  }
});

On Sat, Mar 31, 2018 at 10:48 AM, <martinkovac...@gmail.com> wrote:

> 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": [
>                "t...@test.de",
>                "t...@test2.de"
>            ]
>        },
>        "1256734": {
>            "states": {
>                "STATEA": 0,
>                "STATEB": 0,
>                "STATEC": 0
>            },
>            "emailContacts": [
>               "t...@test.de",
>               "t...@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! :)
>
>
> --
> 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+unsubscr...@googlegroups.com.
> To post to this group, send email to nodejs@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/nodejs/7e03a57f-0384-424f-97a1-9ed911b8a929%40googlegroups.com
> <https://groups.google.com/d/msgid/nodejs/7e03a57f-0384-424f-97a1-9ed911b8a929%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>



-- 
R. Mark Volkmann
Object Computing, Inc.

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to nodejs@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/CAFfRWnWV%2BSX5O6Sc3NNqn5iXSXUSn_sKj_mMgniRQb9V_KPuWQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to