Native JSON support in AIR 3

One the most exciting things in the latest releases of the Flash runtimes, is the ability to natively work with JSON. Now, I know that the Stage3D feature might be a bit more visually compelling, but any Flash application developer usually has to work with JSON data. If you are not familiar with what JSON is, it stands for Javascript Object Notation, and is quickly becoming the standard method of data transfer with modern web services (like Twitter).

Here is an example of an JSON data file:

   "firstName": "Chris",
   "lastName": "Griffith",
      "elementary": "Franklin",
      "jrhighSchool": "Curran",
      "highSchool": "Bakersfield High",
      "college": "University of California, Santa Barbara"

One of the attractions of using JSON as your data type is it’s readability of the data. So having native support, and hence native performance in working with the data, is a great benefit to developers.


This is the method that will ingest the JSON data and create the native data structure. Here is a brief code snippet that demonstrates loading a JSOn file and tracing out the contents.

var json:URLLoader;
 var parsedJSONData:Object;
function loadMyJSONData():void {
   json = new URLLoader();
   json.addEventListener(Event.COMPLETE, parseJSON);
   json.load(new URLRequest("data.json"));
   trace("Loading JSON file...");
function parseJSON(evt:Event):void {
   trace("JSON file loaded successfully!");
   trace("Parsing JSON...");
   parsedJSONData = JSON.parse(json.data);
   trace("firstName: " + parsedJSONData.firstName);
   trace("lastName: " + parsedJSONData.lastName);
   trace("education.elementary: " + parsedJSONData.education.elementary);
   trace("education.jrhighSchool: " + parsedJSONData.education.jrhighSchool);
   trace("education.highSchool: " + parsedJSONData.education.highSchool);
   trace("education.college: " + parsedJSONData.education.college);

So, you can see that with one line of ActionScript, you have native JSON parsing.

To convert your data into native JSON, it is as simple as calling JSON.stringify(). This method takes in three parameters. The first parameter is the data that you want to convert. There are restrictions to the types of data that can be converted into JSON data. These are Arrays,Strings, Numbers, Booleans, and the null value. The second parameter is an optional filter function or array. You can use this remove elements that are not supported in the JSON format. The final parameter sets the level of spaces that is added before each data pair (thus making it readable).

Here is a simple example:

var myDataObject:Object = new Object();
myDataObject.firstName = "Chris";
myDataObject.lastName = "Griffith";
myDataObject.hasCollegeDegree = true;
var newJSON:Object = JSON.stringify(myDataObject, null, 4);

As someone who often consumes lots of JSON data into my applications, these features are one of these little ‘wins’ that make the next version of the Flash Platform an attractive solution for my development.