Say I have a javascript object that looks like this :
var data = { name: "cliff", age: "34", name: "ted", age: "42", name: "bob", age: "12" } var jsonData = JSON.stringify(data);
I stringify it to convert to JSON. How do I save this JSON to a local text file so I can open it, say, in Notepad etc.
Open a Text editor like Notepad, Visual Studio Code, Sublime, or your favorite one. Copy and Paste below JSON data in Text Editor or create your own base on the What is JSON article. Once file data are validated, save the file with the extension of . json, and now you know how to create the Valid JSON document.
Users can also Convert JSON File to Text by uploading the file. Download converted file with txt extension. JSON to Text Online works well on Windows, MAC, Linux, Chrome, Firefox, Edge, and Safari.
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With