Skip to content

Instantly share code, notes, and snippets.

@Meartan
Created October 11, 2015 03:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Meartan/4366eb342412f809a2fd to your computer and use it in GitHub Desktop.
Save Meartan/4366eb342412f809a2fd to your computer and use it in GitHub Desktop.
JSON prettifier
// The original code from Scott Bush (http://scottbush.net/it/better-living-through-bookmarklets-json-prettifier/)
// I made a little change in it and now you can use it as a bookmarklet both on http and https
// The bookmarklet: javascript:(function()%7Bdocument.body.appendChild(document.createElement(%27script%27)).src%3D%27//rawgit.com/iharosi/1a95074c9e47ae9b649b/raw/18f25c45fcb0610bd8eabd389e1d794c0f13d383/prettifyJSON.js%27%3B%7D)()%3B
function FormatJSON(oData,sIndent){if(arguments.length<2){var sIndent="";}
var sIndentStyle=" ";var sDataType=RealTypeOf(oData);console.log(sDataType);if(sDataType=="array"){if(oData.length==0){return"[]";}
var sHTML="[";}else{var iCount=0;$.each(oData,function(){iCount++;return;});if(iCount==0){return"{}";}
var sHTML="{";}
var iCount=0;$.each(oData,function(sKey,vValue){if(iCount>0){sHTML+=",";}
if(sDataType=="array"){sHTML+=("\n"+sIndent+sIndentStyle);}else{sHTML+=("\n"+sIndent+sIndentStyle+"\""+sKey+"\""+": ");}
switch(RealTypeOf(vValue)){case"array":case"object":sHTML+=FormatJSON(vValue,(sIndent+sIndentStyle));break;case"boolean":case"number":sHTML+=vValue.toString();break;case"null":sHTML+="null";break;case"string":sHTML+=("\""+vValue+"\"");break;default:sHTML+=("TYPEOF: "+typeof(vValue));}
iCount++;});if(sDataType=="array"){sHTML+=("\n"+sIndent+"]");}else{sHTML+=("\n"+sIndent+"}");}
return sHTML;}
function SortObject(oData){var oNewData={};var aSortArray=[];$.each(oData,function(sKey){aSortArray.push(sKey);});aSortArray.sort(SortLowerCase);$.each(aSortArray,function(i){if(RealTypeOf(oData[(aSortArray[i])])=="object"){oData[(aSortArray[i])]=SortObject(oData[(aSortArray[i])]);}
oNewData[(aSortArray[i])]=oData[(aSortArray[i])];});return oNewData;function SortLowerCase(a,b){a=a.toLowerCase();b=b.toLowerCase();return((a<b)?-1:((a>b)?1:0));}}
function RealTypeOf(v){if(typeof(v)=="object"){if(v===null)return"null";if(v.constructor==(new Array).constructor)return"array";if(v.constructor==(new Date).constructor)return"date";if(v.constructor==(new RegExp).constructor)return"regex";return"object";}
return typeof(v);}
if(typeof jQuery=="undefined"){var jQ=document.createElement("script");jQ.type="text/javascript";jQ.onload=runthis;jQ.src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js";document.body.appendChild(jQ);}else{runthis();}
function runthis(){var unformattedJSON=$(document).find("pre").text();try{$(document).find("pre").text(FormatJSON(JSON.parse(unformattedJSON)));}catch(e){window.console&&console.log("Could not parse JSON: "+e.message+"; Perhaps this page isn't JSON?");}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment