3

I have a form on submit of the form I am receiving the json object after form submit. What I am trying is that I have a file data.json in my application folder in "/src/app/data.json". How can I write the data into the JSON file with Node.

Below is my code

Below is the code in my component.ts

 savedatatojson(jsonobject){
   // jsonobject is the json data
   this.dataservice.savedata(jsonobject).subscribe(data => {
     console.log(this.data)
  })
 }

Below is the code in my service file

 public savedata(jsonobject):Observable<any>{
   //code to be written
 }

How do I save data to json file which I have obtained in my service file

rji rji
  • 697
  • 3
  • 17
  • 37
  • What does "local JSON file" mean? Do you want to save a file on the *server* where the application is deployed, or on the machine of the user using your application? Note that neither the server nor the client will have an `src/app/` folder. This folder only exists on your machine. – JB Nizet Sep 01 '19 at 10:28
  • @JBNizet Yes, that exists only in my machine and how to save in my machine – rji rji Sep 01 '19 at 10:55
  • You can't. A user of the application will use its own machine to browse the web, and the application will be deployed on a server, not on your machine. S your machine is ompletely irrelevant. – JB Nizet Sep 01 '19 at 11:01

1 Answers1

4

You can't, web browsers don't have permissions to write to source files. There does exist a file system api, however this is non-standard and can only access a very limited location of files.

Instead you should either send the data to a database which later you can read and write to a file, or generate a file within the application which users could download for themselves to their own computer downloads folder. There is also the option to use serverless functions to update a remote file on a server, or for example a google sheet.

chrismclarke
  • 1,995
  • 10
  • 16