Monday, May 25, 2009

JSON to Flash: YouTube Case Study

I had to implement a custom YouTube gadget in Flash. The term gadget does not refer to a specific gadget technology, such as Google Gadgets. YouTube Gadget refers only to small web applications, implemented in HTML or Flash, that are displayed within an iFrame on a YouTube brand channel.

And for that purpose I had to fetch the list of videos published by a particular YouTube user which can only be fetched in the form of JSON, RSS or Atom Feed. I decided to go with JSON, initially it was difficult for me to fetch the appropriate values I needed because the JSON returned by YouTube is quite complex in structured but finally I was able to sort it out.

JSON stands for JavaScript Object Notation and is mainly used to transmit such structured data over a network connection.

"firstName": "Atul",
"lastName": "Narang"

In my case the JSON has to be parsed from the following URL -

If you change the last parameter in the URL which is "author=designworxz" to "author=your youtube username" it will fetch list of videos published by you in the form of JSON.

The great community over at have created a JSON parsing library for AS1, AS2 & AS3. But we will focus on Flash CS3 & AS2.0. You can find that parse class here:

Below is the code to parse the JSON returned by YouTube -

import JSON;

var jsondata:LoadVars = new LoadVars();

jsondata.onLoad = function(success) {

if (success) {
trace("load successful");
var o:Object = json.parse(
var s:String = json.stringify(o);
} else {
trace("unable to load JSON data");


The variable "s" will display the complete JSON returned by YouTube in the Output window & then the only thing you have to do is to fetch the required String, Variables & Values from the returned JSON.

Good Luck!!