My question is very similar to this one .

There is a file with information in .json, which is stored next to main.html. In main.html I wrote the editor of this json.

The idea is that I download my own file (sorry, I haven’t found it easier, I will be happy for help), change it in the editor, and then overwrite it again.

Downloading and processing:

function loadJSON(callback) { var xobj = new XMLHttpRequest(); xobj.overrideMimeType("application/text"); xobj.open('GET', '../data.json', true); // Replace 'my_data' with the path to your file xobj.onreadystatechange = function () { if (xobj.readyState == 4 && xobj.status == "200") { // Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode callback(xobj.responseText); } }; xobj.send(null); } document.getElementById('submit').addEventListener('click',function() { // Get the value from the editor var userJson = editor.getValue(); console.log("userJson:", userJson); loadJSON(function(response) { var previousJson = JSON.parse(response); console.log("previousJson:", previousJson); var newJson = previousJson; var days = ["Понедельник", "Вторник", "Среда", "Четверг", "Пятница", "Суббота"]; var i = 0; for (var i = 0; i < 6; i++) { newJson[userJson["Класс"] + "_lessons"][i] = userJson[days[i]]; } console.log("newJson:", newJson) // тут должно быть сохранение }); }); 

The problem is that I do not know how to overwrite.

That is: there is a variable newJson , which must be output to the file " ../data.json ". What should I do?

  • Make your question look like that. I still strongly doubt that you have a server running at all. - Artem Gorlachev
  • @ArtemGorlachev, while I just open the html localhost page on my computer, then I will - Oskar Sharipov
  • Globally, the answer to your question is to send it back to the json server as string - JSON.stringify(data) , on the server accept it and fs.writeFile - Artem Gorlachev
  • How is that not clear? do you know php ? - Raz Galstyan
  • Where is your code? let it not work, give what you have already done - Raz Galstyan

0