Pratik Naik
Pratik Naik

Reputation: 410

I want to Update a .json file please tell me how to go about it

Following is the code: I am writing a function to update the color attribute in json file. I am able to update the Object but can't write it back to the file(Modify the file). I need to update json data file when user gives input via form.

 function updatecolor(id, color) {
   $.ajax({
     type: "GET",
     dataType: "json",
     url: "Data.json",
     cache: false,
     beforeSend: function() {
       $('#table').html('loading please wait...');
     },
     success: function(jsondata) {
       console.log(jsondata);
       count = jsondata.length;
       for (i = 0; i < jsondata.length; i++) {

         if (jsondata[i].id == id)
           jsondata[i].color = color;


       }

       window.alert(JSON.stringify(jsondata));
     }
   });
 }

 function popupform() {
   $('#formpop').show();
 }

 function my() {
   $.ajax({
     type: "GET",
     dataType: "json",
     url: "Data.json",
     cache: false,
     beforeSend: function() {
       $('#table').html('loading please wait...');
     },
     success: function(jsondata) {
       console.log(jsondata);
       count = jsondata.length;
       var str = '';
       var str2 = '';
       var str3 = '';
       //str += '<ul>';
       $.each(jsondata, function(idx, obj) {
         var match = obj.Color;
         if (match == "Blue") {
           str += 'ID :' + obj.id + '    Color : ' + obj.Color + '<br> ';
         }
       });
       $.each(jsondata, function(idx, obj) {
         var match = obj.Color;
         if (match == "Red") {
           str2 += 'ID :' + obj.id + '    Color : ' + obj.Color + '<br> ';
         }
       });
       $.each(jsondata, function(idx, obj) {
         var match = obj.Color;
         if (match == "Green") {
           str3 += 'ID :' + obj.id + '    Color : ' + obj.Color + '<br> ';
         }
       });
       //str += '</ul>';
       $('#abc').html(str);
       $('#abc2').html(str2);
       $('#abc3').html(str3);
     }

   });
 }

Edit - Adding the server code here from the comment section:

var http = require("http"); 

var fs = require("fs"); 

function send404Response(response){ 
  response.writeHead(404, {"Content-Type": "text/plain"}); 
  response.write("Error 404 - Page not found"); 
  response.end(); 
} 

function onRequest(request, response) { 
  if( request.method == 'GET' && request.url == '/' ){ 
    response.writeHead(200, {"Content-Type": "text/html"}); //Open file as readable stream, pipe stream to response object 
    fs.createReadStream("./index.html").pipe(response);
  }else{
    send404Response(response); 
  } 
} 

http.createServer(onRequest).listen(8888);

Upvotes: 0

Views: 60

Answers (1)

Ethan22
Ethan22

Reputation: 757

https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback

Node.js includes a function for writing data to a file. Use fs.writeFile() to write to the file you want. Note that it will replace the file if the file already exists.

There is also a fs.write() function that looks like you can append to the end of an existing function.

Upvotes: 1

Related Questions