javascriptjson

Write json object to .json file in JavaScript


var proj = {
    Name : "abc",
    Roll no : 123
};

How can I write proj data in JSON format in .json file in javascript?


Solution

  • JSON.stringify (introduced in ES 5.1) will convert the object into a string of JSON.

    var json = JSON.stringify(proj);
    

    JavaScript has no built-in mechanism for writing to a file. You generally need something non-standard provided by the host environment. For example, Node.js has the writeFile method of the File System module.