0

I want to push an data to array in external local JSON file using jQuery.

So any ideas?

I have tried this:

$.getJSON('test.json', function(data) {
data.push('Something');
});

And it wont be pushed into local JSON file

Ayad Hussein
  • 11
  • 1
  • 2
  • u mean local json file ? – Vignesh Raja Jan 25 '18 at 11:24
  • Yes that was i meant – Ayad Hussein Jan 25 '18 at 11:25
  • 2
    What have you tried to achieve your wanted results? What has your research concerning your problem shown? Can you provide code of your tries? [How do I ask a good question](https://stackoverflow.com/help/how-to-ask), [How much research effort is expected](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) and [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) might be helpful to improve your question. – Geshode Jan 25 '18 at 11:25
  • @VigneshRaja Do you know how to make that work? – Ayad Hussein Jan 25 '18 at 11:29
  • @AyadHussein jQuery is not meant for loading/writing any files locally. Browsers enforce security-related limitations accessing the local filesystem (and quite rightly so). Even if you turn off these restrictions for your browser, these aren't the right tools for the job. – DJDaveMark Jan 25 '18 at 11:34
  • At least show a sample of the file contents, and the modification you want to apply (before/after). And give us an example of where an example file would be. – DJDaveMark Jan 25 '18 at 11:41
  • @AyadHussein Explaining why you want to do this might help us too. – DJDaveMark Jan 25 '18 at 11:51

3 Answers3

1

You can do this in JavaScript with node.js (or a multitude of other languages/platforms) but not with a browser & jQuery.

Here's how to read and write a json file in node.js

On the other hand, users could upload a JSON file to your server, where you modify the structure and send them a modified JSON file back as a download.

DJDaveMark
  • 2,669
  • 23
  • 35
0

Is not possible to write files using Javascript (can cause security problems through xss, csrf ..)

No, JavaScript doesn't have access to writing files as this would be a huge security risk to say the least. If you wanted to get/store information server- side, though, you can certainly make an Ajax call to a PHP/ASP/Python/etc. script that can then get/store the data in the server. If you meant store data on the client machine, this is impossible with JavaScript alone. I suspect Flash/Java may be able to, but I am not sure.

If you are only trying to store a small amount of information for an unreliable period of time regarding a specific user, I think you want cookies. I am not sure from your question what you are trying to accomplish, though.

Read/write to file using jQuery

Community
  • 1
  • 1
Cristian Batista
  • 283
  • 3
  • 17
0

You cannot access the files in a local client's machine. May be for development setup you can do it. But before you need to restart the browser with file-access flag set.

You can see my answer here that describes the opening browser setup with the flag set.

Then, you can use the following code to read the data.

var data = [];
var url = "/Users/Vignesh/Desktop/test.json";
var req = new XMLHttpRequest();
req.open("GET",url,true);
req.onreadystatechange=function(){
    if(req.readyState === 4)
    {
        data = JSON.parse(req.responseText);
    }
};
req.send();

To write into the file you may look into this. (Not sure it is working or not)

Vignesh Raja
  • 7,927
  • 1
  • 33
  • 42