0

I need to prettify some JSON to display within an HTML <pre> section.

The working javascript code I use is..

function transformJson(k, v) {

  if (k === 'href' && typeof v === 'string') {
      var label = v.replace(/&/gi, '&amp;');
    return '<a href=' + v + '>' + label + '</a>';
  }
  return v;
}

function jsonFormat(jsonString) {

   var jsonObj = JSON.parse(jsonString, transformJson);
   return JSON.stringify(jsonObj, undefined, 2)
            .replace(/\s"(\w*)":/g, ' "<span class="key">$1</span>":')
            .replace(/:\s"(.*)"/g, ': "<span class="string">$1</span>"');
};

Now I would like to make all attributes keys at the first level, regardless of the attribute value, links to "/documentation#attributeKeyText".

var jsonToPrettify = {
  "href": "link/me",
  "nonHrefButMakeThisKeyALink": "some_text",
  "obj": {
    "href": "link/me",
    "thisKeyWontBeALinkInsteadBecauseHasAParent": "some_text"
  }
}

console.log( jsonFormat( JSON.stringify( jsonToPrettify ) ) );

How can I achieve that? How can I check that the current attribute has no parent object?

Thanks

UPDATE:

Output of the current version is:

{
  "<span class="key">href</span>": "<span class="string"><a href=link/me>link/me</a></span>",
  "<span class="key">nonHrefButMakeThisKeyALink</span>": "<span class="string">some_text</span>",
  "<span class="key">obj</span>": {
    "<span class="key">href</span>": "<span class="string"><a href=link/me>link/me</a></span>",
    "<span class="key">thisKeyWontBeALinkInsteadBecauseHasAParent</span>": "<span class="string">some_text</span>"
  }
}

So I just want the span nonHrefButMakeThisKeyALink to be a link instead..

3
  • What's the point of stringifying the object to JSON and then parse it and then stringify it again? Commented Apr 14, 2016 at 17:34
  • I receive the json in string form from another service. That console.log is just to allow to copy the whole question in the console and see it in action.. Commented Apr 14, 2016 at 17:36
  • This could help.. stackoverflow.com/questions/13518762/… Commented Apr 14, 2016 at 22:29

2 Answers 2

1

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, '&amp;');
    return '<a href=' + v + '>' + label + '</a>';
  } 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, '&amp;');
        v[x] = '<a href=/documentation#' +  x  + '>' + label + '</a>';
      }
    }
  }
  return v;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Simply adding this after JSON.parse did the trick..

var newObj = {};

for(var attr in jsonObj){
    if( jsonObj[attr].constructor != Object && attr != 'href'){
        newObj['<a href="/documentation#'+attr+'">'+attr+'</a>'] = jsonObj[attr];
    }else{
        newObj[attr] = jsonObj[attr];
    }
}

(And then clearly JSON.stringify applied to newObj)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.