2

I am having trouble to figure out that how can I write in a certain place? I want to write where ReadData.welcome_message[0] this command points out.

My database.json file has this;

{"faqs":["Birkere bir?","bir"],"welcome_message": ["welcome"]}

And ReadData.welcome_message[0] points out welcome

    const fs = require('fs'); //imports node.js FileSystem
    const ReadDatabase = fs.readFileSync('database.json'); //reads the file in synchronized way
    const ReadData = JSON.parse(ReadDatabase); //parses bits so it can be readable

    //These three for me to understand what is where
    console.log(ReadData.faqs[0]);
    console.log(Object.keys(ReadData));
    console.log(ReadData.welcome_message[0]);


    let edited_welcome = JSON.stringify(edited_message);
    fs.writeFileSync('database.json', edited_welcome)//I understand this is the way to write to the file
    //console.log('"' +edited_message + '"'); //did help me understand if my code worked
   });
erikvimz
  • 5,256
  • 6
  • 44
  • 60
Haydar Öztürk
  • 122
  • 1
  • 13
  • 2
    Instead of giving a negative vote, answer if you can! I am struggling with this for hours and searching for answers. Couldn't find anything I could solve my problem. I understand maybe you guys are too good, but this is not the way of incentivizing the people new in coding. – Haydar Öztürk Jul 06 '18 at 09:53
  • 1
    This might give you bit of an insight may be https://stackoverflow.com/questions/10685998/how-to-update-a-value-in-a-json-file-and-save-it-through-node-js – Devang Naghera Jul 06 '18 at 10:27

1 Answers1

2

You can update as you need like this:

const ReadDatabase = fs.readFileSync('database.json'); //reads the file in synchronized way
const ReadData = JSON.parse(ReadDatabase); //parses bits so it can be readable

//These three for me to understand what is where
console.log(ReadData.faqs[0]);
console.log(Object.keys(ReadData));
console.log(ReadData.welcome_message[0]);

ReadData.welcome_message[0] = 'New Welcome Message'; // Modify as you need!

let edited_ReadData = JSON.stringify(ReadData);
console.log('Updated database.json: ', edited_ReadData);
fs.writeFileSync('database.json', edited_ReadData);
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40
  • I am not sure if I understood. How does this change the database.js file? ReadData.welcome_message[0] = "" this does not update it. And the code below doesn't write anything new because Edited_ReadData is not defined yet? What do I miss? – Haydar Öztürk Jul 06 '18 at 10:45
  • So all we're doing here is loading the contents of 'database/.son' in the ReadData object. Then we modify the field ReadData.welcome_message[0]. then we write the result to 'database.json'. I'll update the code a little! – Terry Lennox Jul 06 '18 at 10:49
  • So now I log the updated JSON before we write to the database.json file. – Terry Lennox Jul 06 '18 at 10:52
  • I think I explained myself poorly. Since I am not experienced in asking right questions, I couldn't get a right answer my friend. This code does not update the welcome_message value "welcome", I need to be able to update that perminantly so next time that code will executed it will update it with something new again. I am looking at the edited code for 14 min to see if I am wrong, I could still be wrong but I can't see a way of this code doing what I wanted to do. :( – Haydar Öztürk Jul 06 '18 at 11:06
  • 1
    I am so sorry. It is the correct way of fixing this. I just didn't understand it, and I still don't to be honest. But as long as my code works, If I defined this works in my mind no need to understand it :) Thank you very much for the response. – Haydar Öztürk Jul 06 '18 at 11:27
  • 1
    It's not too hard really, just focus on what each line does. You'll learn really quickly!! – Terry Lennox Jul 06 '18 at 15:30