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?
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).
The JSON.stringify
has three parameters, you can use third parameter for that
JSON.stringify(json, null, 4);
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