I need to prettify some JSON to display within an HTML section.
The working javascript code I use is..
function transformJson(k,
Topmost object is passed to function provided as parameter for JSON.parse() under empty key.
You need to include that in your transformJson
:
function transformJson(k, v) {
if (k === 'href' && typeof v === 'string') {
var label = v.replace(/&/gi, '&');
return '' + label + '';
} else if (k === '') {
for (var x in v) {
//skipping 'href' because it's handled by previous 'if'
if (x !== 'href' && typeof v[x] === 'string') {
var label = v[x].replace(/&/gi, '&');
v[x] = '' + label + '';
}
}
}
return v;
}