PDA

View Full Version : JSON Browser


rnd me
11-25-2008, 07:33 AM
often i find it difficult to navigate through dense json structures.

when trying to code callbacks for JSON web services like weather (http://ws.geonames.org/findNearByWeatherJSON?lat=40&lng=-88), news (http://pipes.yahoo.com/pipes/9oyONQzA2xGOkM4FqGIyXQ/run?&_render=json&_callback=rssCB&feed=http%3A%2F%2Fwww.nytimes.com%2Fservices%2Fxml%2Frss%2Fnyt%2FPolitics.xml)
, and twitter (http://search.twitter.com/search.json?q=javascript)for example, it is hard to tell where in the structure you are by looking at chunks of text.

I wrote this simple JSON browser to help out.
It binds a JSON structure to navigational and display form controls.

you can also use the online version (http://danml.com/pub/jsonBrowser.htm)

3.3KB:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Dan's JSON browser</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type='text/css'>
#view { font-size: 120%;}
#tree { width: 10em; }
</style>
</head>
<!-- by Dan Davis, 2008. I, dan davis, hereby place this page (jsonBrowser.htm) into the public domain. Enjoy!-->
<body onload="boot()">
<h3>Dan's JSON Browser</h3>

<input type='button' onclick='navUp()' value='Up' /> Path <input type='text' id='paf' value='' size="88" /> <br />

<select size='12' id='tree' onchange="navTo();">
<option>Loading Object Tree</option>
</select>

<textarea rows='10' cols='80' id='view' onfocus="this.select()"></textarea>

<br />
Source:<br />

<textarea rows='16' cols='110' id="json" onfocus="this.select()">
//from an example on http://www.json.org/example.html
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}</textarea>

<br />

<input type='button' value='Load new JSON' onclick="loadJSON()" /> <br />

<script type='text/javascript'>
//library functions
function obKeys(ob) {var r = [];var i = 0;for (var z in ob) {if (ob.hasOwnProperty(z)) {r[i++] = z;}}return r.sort();}
function el(tid) {return document.getElementById(tid);}
function escJS(tes) {var tes2 = tes.replace(/([\r\n])/gm, "");var qw = tes2.replace(/(["'\f\b\n\t\r\/\\])/gm, "\\$1");return qw;}

//globals:
var tree,paf,ops,view;


function loadJSON(txt){
txt = txt || el('json').value;
doLoad(eval('('+txt+')'));
}


function navUp(){
var np = paf.value.split(/\[/g).slice(0,-1).join("[");
tree.paf = np;
drawTree();
}


function navTo(){
var o = ops[tree.selectedIndex];
var kn = o.text;
paf.value = tree.paf;
if(o.branch){
tree.paf = tree.paf + "[\""+kn+"\"]";
drawTree();
return;
}
view.value = String( tree.branch[ kn ] );
}



function buildTree(ob){
tree.paf="";
tree.ob = ob;
drawTree();
}

function drawTree(){
var ob = tree.ob;
paf.value = tree.paf;
var branch = ob ;
if( tree.paf ){
branch = eval( "tree.ob" + tree.paf );
//using eval to avoid n-level select case/Array syntax branches- eval should support infinite levels.
}
tree.branch= branch;
var kds =obKeys( branch ) ;
ops.length=0;
for(var b=0,mx=kds.length;b<mx;b++){
var a = kds[b];
ops[b]=new Option(a);
if(typeof branch[a]==="object"){ ops[b].style.color="blue"; ops[b].branch=true; }
}

}

function doLoad(ob){
var json= ob;
if( json ){ buildTree(json);}
}


function boot(){
tree=el("tree");
paf = el("paf");
view=el("view");
ops=tree.options;
loadJSON();
}

</script>
</body>
</html>




I hope noobies can learn a little about handling JSON by poking around the source of this copyright-free x-browser application.

It's free, so feel welcome to use the page as a template for many kinds of projects from recipe collections, address books, etc...
just hard-code the json into a variable, pass that variable as an argument to loadJSON in boot(), and you are up and running!


Please leave comments/complaints/requests and especially improvements on/about this below, I'd like to hear from you.

ironflex
11-28-2008, 10:23 PM
There are several ways to verify the structure and data types inside a JSON object, much like an XML schema. JSON Schema is a specification for a JSON-based format for defining the structure of JSON data. JSON Schema provides a contract for what JSON data is required for a given application and how it can be modified, much like what XML Schema provides for XML.
--------------------
ironflex
Link Building (http://www.widecircles.com)

itsallkizza
11-29-2008, 08:19 AM
That's pretty cool :)

pjorstad
01-18-2009, 08:47 PM
There are several ways to verify the structure and data types inside a forums JSON object, much like an XML schema. JSON Schema is a specification for a JSON-based format for defining forum the structure of JSON data. JSON Schema provides a contract for what JSON data is required for a given application and how it can be modified, much like what XML Schema provides for XML.
--------------------


Great Post thanks.


______________________
muscle forums (http://www.musclesweb.net/forum/) | powerlifting forum (http://www.weightliftingforum.net)

Sephr
01-19-2009, 03:39 AM
No offense, but that JSON browser is terribly insecure. Never eval() JSON without preprocessing it. I recommend you feature check the browser to see if it has built-in JSON (like IE8 and Firefox 3.1) and load json2.js (http://www.json.org/json2.js) if it is not detected.

rnd me
01-19-2009, 03:22 PM
No offense, but that JSON browser is terribly insecure. Never eval() JSON without preprocessing it. I recommend you feature check the browser to see if it has built-in JSON (like IE8 and Firefox 3.1) and load json2.js (http://www.json.org/json2.js) if it is not detected.

None taken: It is not "insecure" at all, that's just not true...

This is a tiny utility, the source of which could be printed on a tshirt. There is no login or password to steal, no keys to catch, no credit card information in play. In short, it wouldn't much matter even if malicious code got executed inside. The risks with json stem from using it poorly inside of a sensitive application. Other than those situations, it's no more dangerous than any other javascript people cut and paste.

And that's overlooking the fact that it's you who pastes the json into the app in the first place. Why would someone choose this app to test malicious code? why would someone paste shady code in anyway, and what harm could it do? Someone can always use firebug to do the same thing...


In practice, i don't think too many json data sources are compromised or contain invalid json.


as far as json2 and parse:
I thought it was a great idea when i first saw it, it's pretty impressive code within.

Unfortunately, i don't find it terribly useful in practice. It's good for ASP3 jscript, but not too good for client-side use. It needs a string. Strings must come from your own domain, which is presumably confident and safe. .Parse is useless in situations where it would be the most beneficial; filtering incoming remote data through jsonp.

ironflex
08-17-2009, 11:51 PM
how are you guys ?

after quite long time i am posting on this forum and really guys this forum is so good
i am glad to back on it
seo jaipur (http://blogshubhinetwork.com)

pjorstad
11-23-2009, 01:18 AM
I agree with rnd me. Its secure enough. Ive looked and it doesn't seem any more dangerous than your average code.


____________________
funny stuff (http://www.funnystuffblog.com)
cat supplies (http://www.kittyitems.com)

martingh
07-23-2010, 08:31 PM
Thanks rnd, I am really helpful by your post and going to use your code to test it out whether it will work or not. Also, I would like to thank all you guys here sharing their knowledge.

Thanks,
Ranking By SEO India (http://www.rankingbyseo.com)