created by Aha00aAha00a at 2015-10-31
last modified by Aha00aAha00a at 2018-03-12
revision: 2

JSON PrettyPrint

function jsonPrettyPrint(json) {
    return $('<pre class="json"/>').html(
        json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
            .replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
                var cls = 'number';
                if (/^"/.test(match)) {
                    if (/:$/.test(match)) {
                        cls = 'key';
                    } else {
                        cls = 'string';
                    }
                } else if (/true|false/.test(match)) {
                    cls = 'boolean';
                } else if (/null/.test(match)) {
                    cls = 'null';
                }
                return '<span class="' + cls + '">' + match + '</span>';
            })
    );
}
pre.json {
	border: 1px solid #ccc;
	background-color: #eee;
	padding: 10px;
	font-family: monospace;
	line-height: 150%;
	overflow-x: scroll;

	.string { color: #f00; }
	.number { color: #0c0; }
	.boolean { color: #00f; }
	.null { color: magenta; }
	.key { color: #444; }
}

1. See Also

1.2. Adjacent Pages

≤ 32
all
1.0x
1.0x
80
-120
ON
0/0
0/0
0.00
0
0 / 0
1.00x