Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a json object to a text file in javascript

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

like image 877
kosta Avatar asked Feb 26 '14 23:02

kosta


People also ask

Can I convert JSON to text?

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.

What is toJSON () in JSON?

The toJSON() method returns a date object as a string, formatted as a JSON date.


1 Answers

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."

like image 139
ktm5124 Avatar answered Oct 06 '22 16:10

ktm5124