Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download object as formatted json file

I followed this guide to download a JSON object from the browser. This is what my code looks like:

var json = this.getEditorJSON();

var data = "text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(json));
var a = document.createElement('a');
a.href = 'data:' + data;
a.download = 'resume.json';
a.innerHTML = 'download JSON';

var container = document.getElementById('container');
container.appendChild(a);
a.click();

a.remove();

But this gives me a single line file that is hard to read. Is there an easy way to format it as a readable JSON file, with newlines and indentation?

like image 547
JaskeyLam Avatar asked Apr 28 '16 13:04

JaskeyLam


People also ask

What is JSON format download?

JSON (JavaScript Object Notation, pronounced /ˈdʒeɪsən/; also /ˈdʒeɪˌsɒn/) is an open standard file format and data interchange format that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and arrays (or other serializable values).


1 Answers

The JSON.stringify has three parameters, you can use third parameter for that

JSON.stringify(json, null, 4);
like image 180
isvforall Avatar answered Sep 19 '22 07:09

isvforall