0

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? Commented May 5, 2021 at 20:33

1 Answer 1

0

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.