0

I am building middleware for an API in Javascript. I try to show my Javascript in JSON, but I show only the raw text. I need the JSON format for the external application.

Example code:

var obj = new Object();
   obj.name = "Raj";
   obj.age  = 32;
   obj.married = false;
   var jsonString= JSON.stringify(obj);
   console.log(jsonString);
   document.getElementById("test").innerText = jsonString;

What I get:

pic: What I get now

What I want:

pic: What I want

I hope that somebody can help me :)

Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
  • This chrome extension makes it print out pretty JSON, which makes development much nicer: https://github.com/callumlocke/json-formatter – Jeremy Harris Nov 21 '19 at 13:26

1 Answers1

-1

use JSON.stringify(obj,null,2) where 2 is the indentation size

const obj = {
  foo: 'bar',
};
document.write('<pre>' + JSON.stringify(obj,null,2) + '</pre>');
Apolo
  • 3,844
  • 1
  • 21
  • 51