JSON.stringify output to div in pretty print way
Please use a <pre>
tag
demo : http://jsfiddle.net/K83cK/
var data = { "data": { "x": "1", "y": "1", "url": "http://url.com" }, "event": "start", "show": 1, "id": 50}document.getElementById("json").textContent = JSON.stringify(data, undefined, 2);
<pre id="json"></pre>
My proposal is based on:
- replace each '\n' (newline) with a <br>
- replace each space with
var x = { "data": { "x": "1", "y": "1", "url": "http://url.com" }, "event": "start", "show": 1, "id": 50 };document.querySelector('#newquote').innerHTML = JSON.stringify(x, null, 6) .replace(/\n( *)/g, function (match, p1) { return '<br>' + ' '.repeat(p1.length); });
<div id="newquote"></div>