JSFiddle - React, Tailwind, and code Playground
HTML
<a id="downloadAnchorElem" style="display:none"></a>
JavaScript
var getJSON = function(url, successHandler, errorHandler) {
var xhr = typeof XMLHttpRequest != 'undefined' ?
new XMLHttpRequest() :
new ActiveXObject('Microsoft.XMLHTTP');
xhr.open('get', url, true);
xhr.onreadystatechange = function() {
var status;
var data;
// https://xhr.spec.whatwg.org/#dom-xmlhttprequest-readystate
if (xhr.readyState == 4) { // `DONE`
status = xhr.status;
if (status == 200) {
data = JSON.parse(xhr.responseText);
successHandler && successHandler(data);
} else {
errorHandler && errorHandler(status);
}
}
};
xhr.send();
};
getJSON('https://raw.githubusercontent.com/zemirco/sf-city-lots-json/master/citylots.json', function(data) {
var a = document.createElement("a");
document.body.appendChild(a);
var json = JSON.stringify(data) + "_" + JSON.stringify(data),
blob = new Blob([json], {
type: "octet/stream"
}),
url = window.URL.createObjectURL(blob);
a.href = url;
a.download = "fileDownloadTest.json";
a.click();
window.URL.revokeObjectURL(url);
}, function(status) {
alert('Something went wrong.');
});