You can not write to any file using JavaScript alone. Just cookies or local (or session) storage. You can write JSON into local storage and just use JSON.stringify to serialize a JavaScript object.
localStorage.setItem('myData', JSON.stringify(myData));
And to retrieve the object
var myData = JSON.parse(localStorage.getItem('myData'));
If you really want to write something in the file, you need use the server script like php or asp. Send myData to the php, and php will write what you want.
<?php
$myData = $_GET['myData']
$fp = fopen('myJSONfile.json', 'w');
fwrite($fp, json_encode($myData));
fclose($fp);
?>