0

I found this page How can I pretty-print JSON using node.js? and I think it's useful, but I have one question: I have a page that give a result from a request sparql in an array, and I want to take just one line of this results with a button "add" that is insert in the last column of the line, and when I take the line in Json I want to write it in a file json that already exist with other data. The button call the next function: function add(param) {

      res= param;
    $.ajax({
      type: "POST",
      url: "....",
      data: { nom:resource,abstract:resume,photo:src,indice: res ,fichier:$("#myselect" ).val()}
      })
      .done(function( msg ) {
        alert( "ajout réussie"+msg);
        window.location.reload();
    });

};

Where res is the index for the line that I want to add, and data all data I need to add. So I want to know how I can change this code to use the last code posted by "Larry Battle". I have to put his code in a file "add.js" and I call this file in url? Or How?

Link for my example: https://www.dropbox.com/s/noyh1ltwljlpevw/Capture%20du%202014-05-01%2019%3A05%3A04.png

Community
  • 1
  • 1
user51324
  • 59
  • 1
  • 6

1 Answers1

0

The easiest way might be to require the original json file, add the new data in memory, then save the object back as a pretty json file (overwriting the original if you like).

I'm not sure what you meant by "res is the index of the line I want to add", so I'll assume it's going to be the property name in the javascript object that you serialize to JSON. So in general it'd look something like this:

var fileData = require('/path/to/jsonData.json');
// fileData is now a JS object that was parsed from the json file); this is a sync operation.

fileData[res] = data; // data object from your existing code.

// write fileData to a JSON file like you were going to do before.
Paul
  • 35,689
  • 11
  • 93
  • 122
  • Thanks for your answer. I edited my first post, look for the picture on the link, I hava an array with 1 line sor res=0, if I had 2 line in this array res will be equals 1 (so I can access for the data on line 0 and/or 1). So my question is when I click on the button "ajouter" I want to write all the data on line in format json in a file that already exist with others data. That's why I put a link I found it where is a code to write in a file json, my question is Can I use this code without edit it? or I have to change somethin? And how to use this code in my function. – user51324 May 01 '14 at 17:21
  • Your link isn't working for me. So, my advice was based on the assumption (in your title) that you have a server process running node.js in your architecture. My code is JS that should be in the node.js code somewhere. Your code is jquery based, so it's going to be in the browser. So if your server code will receive the data from the client as your code suggests, then what it has to do is what I suggested. – Paul May 01 '14 at 18:35
  • Ok, but I don't want to overwrite the original, I just want to add a new line in the original, so when you write fileData[res]=data; what does that mean? How its interpreted like a json format? I don't need to use "JSON.stringify"? For exemple fileData[0] is going to write on the line 0 from the file? – user51324 May 01 '14 at 19:03
  • No, read what I said. You first read in the file using `require`. That gives you an in-memory representation of the JSON contents, as a javascript object. You can, at that point, manipulate it however you manipulate a normal JS object; so if fileData is an array, then writing fileData[0] = "something" will as you say replace the first element in the array with the word "something". If you want to insert or append instead, use normal JS array syntax to do so. Once you are ready to save changes, then you do the JSON.stringify stuff and save the resulting string to disk; which file is up to you. – Paul May 02 '14 at 12:20
  • Thank you very much, and in my first post I ask how to use this file where I write var fileData=require('file') etc. Because my first code is in file.html so I put in url the name of file.js? And I start the file.js with node js or how? I don't really understand how to use it. When I send data from ajax in file.html, how to use it, because there are also the name of file, so I hava to separate the data. – user51324 May 02 '14 at 18:37
  • You need to have an application running on the server, using Node. Express.js is a great way to do that. When your client code (the code in jquery you have) posts to the server, the request handler on the server should run the code I provided to you in order to save the data to the file. – Paul May 03 '14 at 18:09