I have a JSON object in javascript. I want to simply write the JSON object to a text file. From the things that I came across so far indicate that it is not possible to do so because of security concerns on the client side. Is there a workaround?. Will it be possible to modify a file already present if put some dummy values initially?
Thanks
How do I convert a JSON file to readable? Open JSON to String tool and Copy and Paste JSON Code in Input Text Editor. If you do have a JSON data file, you can upload the file using the Upload file button. Users can also upload JSON with an internet-accessible URL.
The toJSON() method returns a date object as a string, formatted as a JSON date.
One thing you can do is setup the JSON as a download on the fly.
var data = "{name: 'Bob', occupation: 'Plumber'}"; var url = 'data:text/json;charset=utf8,' + encodeURIComponent(data); window.open(url, '_blank'); window.focus();
Working demo: http://jsfiddle.net/sLq3F/
Apart from that, you can't write a JSON to a file on the clientside due to security reasons. (Otherwise you have access to the filesystems of your website's users.) You would have to use a server-side language for this, and store the file on the server-side.
Correction: Looks like you can write to a file, i.e., a "sandboxed section" of the user's filesystem. See Kevin Jantzer's comment below.
Another Correction: Sorry, the Filesystem API isn't in use. From the HTMl5Rocks website: "In April 2014, it was announced on public-webapps that the Filesystem API spec should be considered dead. Other browsers have showed little interest in implementing it."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With