Download string data as file based on the file type (text/Json)

I am trying to downloading a string data as file such as text, and json files based on the fileType we are passing as parameter. I have tried below code.

function downloadString(text, fileType, fileName) {
  var blob = new Blob([text], { type: fileType });

  var a = document.createElement('a');
  a.download = fileName;
  a.href = URL.createObjectURL(blob);
  a.dataset.downloadurl = [fileType, a.download, a.href].join(':');
  a.style.display = "none";
  document.body.appendChild(a);
  a.click();
  document.body.removeChild(a);
  setTimeout(function() { URL.revokeObjectURL(a.href); }, 1500);
}

Downloading is working. But it always download txt file even if we pass json filetype.
Can anyone help this?