0
votes

I am trying to send an email message using sendgrid API. I want the body of the message to include a json object formatted as HTML.

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}

But the body of the email instead looks like this:

{ "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": { "GlossEntry": { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": { "para": "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": ["GML", "XML"] }, "GlossSee": "markup" } } } } }

When I stringify a json object and send as HTML with the sendgrid API, the email body is just one long text string, which is difficult to read :( I also tried NPM package retty-print-json

All of these produce the same result: the email body is just one long text string

stringify

let html = stringify.JSON(getStats);   
        const msg = {
            to: '[email protected]',         
            from: '[email protected]', 
            subject: datasetTitle,
            html: html
        }

prettyPrintJson

let html = prettyPrintJson.toHtml(getStats);
        const msg = {
            to: '[email protected]',         
            from: '[email protected]', 
            subject: datasetTitle,
            html: html
        }
1
Could you please clarify what is the end result that you are looking for? - Steven Diamante

1 Answers

0
votes

Whitespace including line breaks, collapse in HTML by default. Wrap the pretty-printed JSON in <pre></pre> and the output should be presented correctly. You don't need a package to add the line breaks, just specify the extra params when serializing JSON.stringify(obj, undefined, 4)