Home > Enterprise >  Indent pretty JSON in Google Sheet Apps Script
Indent pretty JSON in Google Sheet Apps Script

Time:11-13

I would like to show an alert with a pretty JSON, but it doesn't get indented well.

Here's the JS code I'm using :

var jsonText = JSON.stringify(jsonObject, null, 2)
SpreadsheetApp.getUi().alert(jsonText);

And here is the indentation I get as a result :

...
"xxx": "",
"xxx": ""
},
{
"xxx": "yyy",
"xxx": "yyy",
"xxx": "",
"xxx": ""
}
]

It's missing the tab indentation, any idea how I could fix this?

CodePudding user response:

It seems normal space \u{0020} characters are removed from SpreadsheetApp Ui.alert() function. You may use any of the other space characters mentioned in enter image description here

Note - You probably cannot copy/paste those non-breaking spaces from my answer - I think you will only end up copying regular spaces, which will not give you the required indentation (they will be collapsed as per standard HTML rules regarding consecutive whitespace).

So, instead, you can manually enter the NBSP characters using your keyboard.

For me on Windows this was by me holding down the ALT key and typing the number sequence 0160.

On a Mac it is opt space.

For more systems, see enter image description here

  • Related