Thursday, July 9th, 2009
Pimping JSON – YQL now offers JSONP-X!
Yesterday's announcement of Yahoo's YQL now supporting insert, update and delete overshadowed another interesting new feature: JSONP-X output.
Here's what it is and why it is useful: YQL data can be returned in XML which is annoying to use in JavaScript (for starters because of crossdomain issues in Ajax). JSON is much easier as it is native to JavaScript. JSON-P makes it even more easy for us to use as JSON data wrapped in a function call allows us to get the data by creating script nodes dynamically.
Where it falls apart is when you want to get back HTML from some system (not on your own server) and use it in JavaScript. You either need to convert the XML to JavaScript and create HTML elements from it or you need to loop through a JSON construct and assemble HTML from it. JSONP-X works around that step for you. In essence it is XML as a string returned inside a JSON object.
XML:
-
<results>
-
<div id="following">
-
<span>
-
<a href="http://twitter.com/codepo8">Codepo8</a>
-
</span>
-
</div>
-
</results>
JSON:
-
{"results":[
-
"div":{
-
"id":"following",
-
"span":{
-
"a":{
-
"href":"http://twitter.com/codepo8",
-
"text":"Codepo8"
-
}
-
}
-
}
-
]}
JSON-P:
-
foo({"results":[
-
"div":{
-
"id":"following",
-
"span":{
-
"a":{
-
"href":"http://twitter.com/codepo8",
-
"text":"Codepo8"
-
}
-
}
-
}
-
]})
JSONP-X:
-
foo({"results":[
-
"<div id=\"following\"><span><a href=\"http://twitter.com/codepo8\">Codepo8</a></span></div>"
-
]})
The way to invoke the JSONP-X output is to provide a format parameter of xml and a callback parameter.
This allows me for example to get the list of people I follow on twitter from my twitter homepage and display it in another document with a few lines of JavaScript without any need of using the API or having a local proxy:
-
<script type="text/javascript" charset="utf-8">
-
function foo(o){
-
var out = document.getElementById('container');
-
var content = o.results[0]
-
out.innerHTML = content.replace(/href="\//g,'href="http://twitter.com/');
-
}
-
</script>
-
<script type="text/javascript" src="http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Ftwitter.com%2Fcodepo8%22%20and%20xpath%20%3D%20%22%2F%2Fdiv[%40id%3D%27following_list%27]%22&format=xml&callback=foo">
-
</script>
More details on this are available in this blog post.












Nifty. Not enough cross-polination of data formats goes on.
I once saw a bunch of techies laugh at an xml document that wrapped a csv. Why is that funny? Csv’s parse a zillion times after and the xml could be used to describe the csv metadata.
Shame on people for not trying more stuff like this.
An even better (though Firefox-only) solution would be XMLP:
foo(
Codepo8
)
I don’t think that example is a proper conversion from (x|ht)ml to json. There is no differentiation between attributes and elements, also the order of elements is not represented appropriately so the meaning is lost.
It’s not that hard to generate XML/HTML from JSON. This just makes it harder to access specific elements or properties, you have to traverse the structure.
JSON return from YQL had been possible before the recent insert/update/delete changes were made. All you needed to do was append &format=json&callback=callback to your YQL url.
With the changing trend of the world, wedding apart from white,Princess Strapless Satin Evening Dress ivory, beige and other traditional colors,are also increasingly popular in recent years, ghetto prom dressespink wedding package,gothic prom dresses such as pink, pink orange, baby blue, purple night, light green and light silver-gray, very soft and pleasing to wedding hair accessoriesthe eye; if you have the courage to try, dark green, purplish red, deep purple, decorated in pink wedding dress, the formation of rich colors,the dress, the effect is very special;Sash Lace Satin Flower Girl Dress, laden with flour colored silk flowers, butterflies, in order to add color.wedding dressIn fact, the wedding is the color of lesser importance, the most important prerequisite is to match the bride’s complexion. Oriental deep and yellowish color, wearing white Mermaid Satin Organdie Wedding Dress,will appear dull, wearing ivory would be more harmonious and natural, baby blue, wedding accessoriespurple night can not be coordinated with the yellow, but pink orange, light green with yellowish color match. As for the skin and rosy, or bronze skin, wearing white will look great, wedding dress
Rest assured when you are purchasing Safety VestAmerican Promotions’
safety products Mesh Safety Vest
stamped with “Safety for Concern”, you are buying workweartop quality safety apparel.
If you would like to advertise your brand, coverallaccomplish the dual purpose
of promoting your Bomber jacket
business while keeping your work place safe.