Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I save JSON to local text file

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.

like image 786
thatOneGuy Avatar asked Dec 08 '15 12:12

thatOneGuy


People also ask

How do I convert a JSON file to a text file?

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.

Can I convert JSON to txt?

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.


1 Answers

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'); 
like image 112
Rafał Łużyński Avatar answered Oct 17 '22 06:10

Rafał Łużyński