If the content is only available in the client, you'll have to send it to the server. Try something like this:
<a id="download_link">Download</a>
$('#download_link').on('click', function(e){
e.preventDefault();
window.location = 'my-handler.php?json=' + myJsonString;
});
// or without jQuery
document.getElementById('download_link').onclick = function(){
window.location = 'my-handler.php?json=' + myJsonString;
};
Then force the download in my-handler.php
, using the data sent to the server as the file content:
// Use equivalent in your server side script if not using PHP
$filename = 'download-'.time().'.json';
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: Binary');
header('Content-disposition: attachment; filename="'.$filename.'"');
exit($_GET['json']);
Using this method, the current window should not be replaced but the download prompt should start, but you can open in a new window if you like.
This is a quick and dirty solution. Note that this will allow a person to type in anything and download a file. It might be wise to at least validate the content first. There may also be issues with the content being too large.