JSFiddle - React, Tailwind, and code Playground
CSS
pre {outline: 1px solid #ccc; padding: 5px; margin: 5px; }
.string { color: green; }
.number { color: darkorange; }
.boolean { color: blue; }
.null { color: magenta; }
.key { color: red; }
JavaScript
function output(inp) {
document.body.appendChild(document.createElement('pre')).innerHTML = inp;
}
function syntaxHighlight(json) {
json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
return json.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>';
});
}
var obj =
{
"results":[
{
"barCity":"Annapolis",
"barState":"MD",
"barZip":"21401",
"recordingPhone":"410-213-1145",
"distance":"2.10",
"longitude":-725464,
"latitude":489914,
"barLong":-725464,
"barLat":489914,
"barLink":"http:\/\/www.bar.com\/bar\/bow-tie",
"barName":"Bow Tie Bar",
"movie":[
{
"barTitle":"Bar Louie",
"Id":"20057095",
"openTimes":[
{
"time":"12:00pm"
},
{
"time":"3:40pm"
},
{
"time":"6:40pm"
}
]
},
{
"barTitle":"Bar Louise",
"Id":"20057095",
"openTimes":[
{
"time":"12:00pm"
},
{
"time":"3:40pm"
},
{
"time":"6:40pm"
}
]
},
{
...