I dont understand, what the return value is back to as string, while the console.log is showing perfect formate, which required.
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 match ;
});
}
var obj = { a: 1, 'b': 'foo', c: [false, 'false', null, 'null', { d: { e: 1.3e5, f: '1.3e5' } }] };
var str = JSON.stringify(obj , undefined, 4);
console.log(syntaxHighlight(str));
$scope.jsonText = syntaxHighlight(str);
HTML
<form name="form" ng-submit="vm.frmFormatJSON()">
<div class="form-group" >
<label>Format JSON</label>
<input type="text" class="form-control " placeholder="{}" ng-model="vm.formatJSON">
</div>
<div class="text-center">
<button type="submit" class="btn btn-primary" ng-click="formatJSON">
Look
</button>
</div>
</form>
Console.log
Output
update as per @Fissio suggestion, its working perfect, but once I update
var str = JSON.stringify(vm.formatJSON, undefined, 4);
its again show in 1 line, also confuse why " " at the begning and end of the output.


<td ng-bind-html="jsonText"></td>or<td>{{jsonText}}</td>, i tried both. Result should be like console.log ,