close

How do I save JSON to local text file

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How do I save JSON to local text file in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How do I save JSON to local text file ?

  1. How do I save JSON to local text file ?

    do I save JSON to local text file P.S. There is no information about json save, but you can do it changing file type to "application/json" and format to .json

  2. do I save JSON to local text file

    do I save JSON to local text file P.S. There is no information about json save, but you can do it changing file type to "application/json" and format to .json

Method 1

Node.js:

var fs = require('fs');
fs.writeFile("test.txt", jsonData, function(err) {
    if (err) {
        console.log(err);
    }
});

Browser (webapi):

function download(content, fileName, contentType) {
    var a = document.createElement("a");
    var file = new Blob([content], {type: contentType});
    a.href = URL.createObjectURL(file);
    a.download = fileName;
    a.click();
}
download(jsonData, 'json.txt', 'text/plain');

Method 2

Here is a solution on pure js. You can do it with html5 saveAs. For example this lib could be helpful: 
Look at the demo: 
P.S. There is no information about json save, but you can do it changing file type to "application/json" and format to .json

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment