Archive for the ‘javascript’ tag
Using Pretty Print to print JSON structure
I needed to build a “test-api” page for my team so that they could test there API without writing too much code. Nothing special, just a page with a textarea where someone puts in a request string, and another field, where we show the response sent by the server. The server response are in JSON format. For simple responses, everything was fine. But in cases, where the response was huge or had a complex structure, we wanted someway to show the structure in a more readable format. Something that helped developers see the hierarchy.
This is where pretty print (by James Padolsey) helped us.
Its dead simple to use:
[code language=”javascript”]
//include prettyprint .js:
//data contains the JSON response sent by the server.
$(“#detail”).append((prettyPrint($.parseJSON( data ), {
maxArray: 100,
expanded: false,
maxDepth: 100
})));
[/code]
E.g. Output: