0

In my web application, javascript code, I have created a JSON object, or actually, list of the json objects. I want to save this into my .json file inside the file structure of my project. How can I do that?

I have this piece of code that actually parse the json and allows to save it, but not automatically, it just popups the window in the browser with the option to save it somewhere by the user:

function saveJSON(text, filename){
            var a = document.createElement('a');
            a.setAttribute('href', 'data:text/plain;charset=utf-8,'+encodeURIComponent(text));
            a.setAttribute('download', filename);
            a.click()
        }
saveJSON( JSON.stringify(result), "filename.json" );
enneenne
  • 209
  • 1
  • 8
  • 1
    I think you should send that JSON to your back-end and let it save the file. You can't save a file from your front-end to your project's file, It can only save to the user's device – Doan Van Thang Feb 09 '21 at 18:15

1 Answers1

0

From a website, you can't access to file system. You can just "download" a file. If you want to persist data, use localstorage.

More info: Local file access with JavaScript