-1

In my application, any error is catch and sent via email for review. I'm currently adding headers and body to the email to help reproduce errors. It's currently displayed in a single row. I would like to display this row in a formatted way so it's easier to read the email.

I'm currently using JSON.stringify(myJsonHere, null, 2) to format my JSON. It does work if I use console.log(JSON.stringify(myJsonHere, null, 2)) to display it, but does not work when I add it to a HTML email like '<b>Headers:</b> ' JSON.stringify(myJsonHere, null, 2).

My goal is to get this format:

{
  data: "test",
  id: 1,
  user: {
    id: 1,
    user: "test"
  }
}

Instead of this format:

{data: "test", id: 1, user: {id: 1, user: "test"}}
Leccho
  • 467
  • 5
  • 23
  • Please add more information! In addition, could you please provide a minimally reproducible example? – Endothermic_Dragon Mar 22 '21 at 15:52
  • It isn't clear what you are doing here. Are you trying to embed a ` – Quentin Mar 22 '21 at 15:52
  • See https://stackoverflow.com/questions/9492249/render-a-string-in-html-and-preserve-spaces-and-linebreaks – Ivar Mar 22 '21 at 15:56

1 Answers1

4

const pre = document.querySelector('pre');

pre.innerHTML = JSON.stringify({ test: 1, nested: { a: 1} }, null, 2)
<pre></pre>
AngelSalazar
  • 3,080
  • 1
  • 16
  • 22