0

I have looked at some issues like: How can I pretty-print JSON using JavaScript? and Javascript: How to generate formatted easy-to-read JSON straight from an object? and http://jsfiddle.net/AndyE/HZPVL/

But I cannot get it to work for me. I must be doing something wrong? Can anyone advise?

I tried adding '\t' to the stringify part, but it did not work for me

$(document).on("click", "#doBackupBtn", function(e) {
    e.preventDefault();
    console.log("Begin backup process");

    $.when(
        backup("allergies")

    ).then(function(allergies, log) {
        console.log("All done");
        //Convert to JSON
        var data = {allergies:allergies}
        var serializedData = JSON.stringify(data, '\t');
        console.log(serializedData);
        (function(console){

console.save = function(data, filename){

    if(!data) {
        console.error('Console.save: No data')
        return;
    }

    if(!filename) filename = 'console.json'

    if(typeof data === "object"){
        data = JSON.stringify(data)
    }

    var blob = new Blob([data], {type: 'text/json'}),
        e    = document.createEvent('MouseEvents'),
        a    = document.createElement('a')

    a.download = filename
    a.href = window.URL.createObjectURL(blob)
    a.dataset.downloadurl =  ['text/json', a.download, a.href].join(':')
    e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
    a.dispatchEvent(e)
 }
})(console)
Community
  • 1
  • 1

2 Answers2

1

The \t is on third argument. Try this on your browser console ( FF Ctrl+Shift+K ) :

console.log(JSON.stringify({"a": {"b": "c"}}, null, 4));

Result:

"{ "a": { "b": "c" } }"

Iqbal Fauzi
  • 1,561
  • 11
  • 12
0

You are missing the JSON.stringify(obj, undefined, 2)

fzzle
  • 1,466
  • 5
  • 23
  • 28