I followed this guide to download a JSON object from the browser. This is what my code looks like:
JavaScript
x
14
14
1
var json = this.getEditorJSON();
2
3
var data = "text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(json));
4
var a = document.createElement('a');
5
a.href = 'data:' + data;
6
a.download = 'resume.json';
7
a.innerHTML = 'download JSON';
8
9
var container = document.getElementById('container');
10
container.appendChild(a);
11
a.click();
12
13
a.remove();
14
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?
Advertisement
Answer
The JSON.stringify
has three parameters, you can use third parameter for that
JavaScript
1
2
1
JSON.stringify(json, null, 4);
2