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

7
  • u mean local json file ? Commented Jan 25, 2018 at 11:24
  • Yes that was i meant Commented Jan 25, 2018 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, How much research effort is expected and How to create a Minimal, Complete, and Verifiable example might be helpful to improve your question. Commented Jan 25, 2018 at 11:25
  • @VigneshRaja Do you know how to make that work? Commented Jan 25, 2018 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. Commented Jan 25, 2018 at 11:34

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.