Learning What I Know

I recently received an e-mail from one of my subscribers that asked a question. The answer may be useful to some so I’m placing it here.

The original question

It seems so interesting developing websites application these days, thanks to the web 2.0 phenomena. I can’t call myself a web developer or anything near that, but I’m interested to learn these kind of [things].


Since you seem to be real world developer to me, I have a couple of questions to ask you. I’ve been messing around with XHTML, CSS, Javascript, PHP and MySQL [for] a couple of months now, and it frustrates me sometimes when I sort of forget things and [can]‘t understand the flow of the design of what I’m trying to achieve. [..]

Based on your experience, do you do everything by your own, i mean there are the front end and the back end [things], do you do both?

Talking of object oriented programming (i guess that’s what prototype, script.aculo.us and ajax are really about), which OOP did u learn first? I [learned] simple Java before but does that come into play for web development as well? I mean, does it help if i go into detail learning Java?

Any other useful advice? I would appreciate your comments. Thank you.

My Reply

Web 2.0 phenomenon…Yeah, its good to stay on top of things like that :)

Development. XHTML, CSS, Javascript, PHP and MySQL…I do it all, however, I did not learn them all at once…but over a series of years. I think the key concept is to really try and keep your logic from your design – keep the MySQL/PHP side of things separate from the XHTML/CSS side of things ( e.g. Ajax, Templating, and the Separation of Layout and Logic). Javascript is a bit of a wildcard and bridges between both the presentation and logic.

You can do the above with and without Object Oriented Programming…although OOP is the preferred method. My first OOP language was Java. Next I branched out to Object Oriented PHP, and finally (last year) I learned Object Oriented Javascript.

All you really need is the OO concept in your head before you branch out and if you’ve used Java, you should be good to go. So to answer your question: “Does it help if you go into detail learning Java.” is this: It won’t hurt, but isn’t necessary to succeed in learning the above languages/tools. When using Object Orientation and switching languages, its all about learning the syntax and the gotchas associated with your chosen language.

The biggest chunks of advice I can give regarding your tools of choice are as follows:


  • keep it simple
  • use ids/classes rather than inline styles


  • live on php.net
  • learn through irritation with your own code…mine goes through constant revisions and optimizations
    - pick up a templating engine to help keep your layout and logic separate. I use XTemplates. I’m thinking about looking into SmartyTemplates….use whatever you wish…but templates save TONS of time.


  • know your data
  • know how to normalize your data
  • download open source applications and look at their database structure for a good idea of what their doing and go from there


The Case For JSON: What Is It and Why Use It?


A Little Background

After my post titled Look Ma, Cross Domain Scripting! a while back, I received a comment that was seeking more information. The commenter posts:

I’m looking at your code and it doesn’t explain exactly how this works, it just provides us with code to use. I’m curious as to what makes JSON work and what differences JSON has from XML. Is there any draw-backs or negative effects of using your solution? Is it a hack that will be fixed in the future?

If you know all about JSON and are wondering why to use it…I’ll tell you right now. Speed. But…if you are curious, I’ll attempt to answer the above questions, show some examples and then I’ll re-iterate Cross-Domain Scripting.

What is JSON?

JSON (or JavaScript Object Notation) is a highly portable data interchange format. While its structure is recognized natively by Javascript (as it is Javascript), its formatting conventions are easily recognized by other C-like languages. JSON.org has an excellent description:

JSON is built on two structures:

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

These are universal data structures. Virtually all modern programming languages support them in one form or another. It makes sense that a data format that is interchangable with programming languages also be based on these structures.

In JSON, they take on these forms:

  • An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).
  • An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).
  • A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.
  • A string is a collection of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string.

Excepting a few encoding details, that completely describes the language.

JSON Structure

Here is a graphical representation of the formatting structure (image from JSON.org):


Why JSON over XML?

Alright…I can already see the hordes of people wanting to wrench my head from my neck and drive lollipop sticks up my nose. But take a step back, put down the weapons and hear me out.

XML is versatile. You can create tags to your hearts content and endlessly nest data in seemingly sensible ways. XML is well known and most savvy developers can manipulate it in all sorts of ways. So what isn’t there to like about it? It just so happens that XML is like a fat tick after a good meal…bloated. Despite its ease of creation, the amount of busy tags makes it difficult to read with the human eye. Ok…don’t get me wrong, its easier to read than a Comma Separated file, but when compared to JSON, XML makes my head hurt.

JSON is simple and can represent the exact same data with fewer characters. How? Well, there are no tags. You give a member a name and then a value. Lets compare some examples…

JSON Examples

Lets say I want to expose two quotes from my friends at Uber-Geeks in some way…lets compare them in XML and JSON.

  <quote>"50 degrees all week today." - Ironmule</quote>
  <quote>"He wasn't naked...he had sneakers on." -Jon Emmons</quote>
    "\"50 degrees all week today.\" - Ironmule",
    "\"He wasn't naked...he had sneakers on.\" -Jon Emmons"

That doesn’t seem too different…lets try something a little more complex. How about a Konfabulator Widget?

    <window title="Sample Konfabulator Widget">
    <image src="Images/Sun.png" name="sun1">
    <text data="Click Here" size="36" style="bold">
            sun1.opacity = (sun1.opacity / 100) * 90;
{"widget": {
    "debug": "on",
    "window": {
        "title": "Sample Konfabulator Widget",
        "name": "main_window",
        "width": 500,
        "height": 500
    "image": { 
        "src": "Images/Sun.png",
        "name": "sun1",
        "hOffset": 250,
        "vOffset": 250,
        "alignment": "center"
    "text": {
        "data": "Click Here",
        "size": 36,
        "style": "bold",
        "name": "text1",
        "hOffset": 250,
        "vOffset": 100,
        "alignment": "center",
        "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"

The more complex the data you are trying to represent, the sexier JSON looks in comparison. Check here for some more examples…one is really long and looks quite sexy and readable in JSON and not so much in XML.

JSON and the Ajax Buzz

Ok, so if JSON and XML are pretty much peas in a pod when it comes to what they can represent, why make the switch (or at least…provide JSON). Ajax is our reason! You see, in an Ajax environment where we make calls to web services we expect to get some data back in some form. Well, if we receive XML back as a direct result of an Ajax call, we have to send that data through an XML parser before we can even begin to manipulate the data to be useful to JavaScript. If we receive the data in JSON…we don’t have to do anything but assign the results to a variable because JSON is already JavaScript. From there, we can manipulate the data as normal.

As Ajax methodologies (and even Cross Domain Scripting) become more commonplace, data format becomes vital to the efficiency of our applications. The Zimbra folks seem to have realized this and subsequently re-architected their entire application. Oh, and Script.aculo.us creator Thomas Fuchs seems to agree and states his opinion on XML in Ajax communication: Its dog slow…don’t do it.

JSON and Cross Domain Scripting

In my article Look Ma, Cross Domain Scripting, I discuss a method for doing Ajax-like calls to remote domains. As many of you may or may not know, XMLHttpRequest is restricted from making requests to remote domains.

The way it works is this: Somewhere out there exists a server side script that you wish to access in an Asynchronous fashion. Rather than attempting an XMLHttpRequest (which we know won’t work), all you do is dynamically create a <script> tag with its src set to the URL of the off-site page. When a script tag is inserted, browsers immediately execute the src! As long as the off-site page returns valid JSON, the JSON is executed locally.

For dynamically adding script tags, I use this library found over at Dan Theurer’s Blog. Its pretty simple and prevents duplicate script tags (which is a necessity).

Yahoo! sets a good example. They allow you to pass in two parameters to any of their web services (which return XML by default) and get the results in JSON.

Get JSON Output with output=json

By default the Yahoo! Web Services return output in XML format. To get output in JSON format, use the output=json parameter in the request:

http://api.search.yahoo.com/ ImageSearchService/V1/ imageSearch?appid=YahooDemo &query=Madonna&results=2 &output=json

Add a Callback with callback=function

The callback parameter (callback=function) wraps the JSON output text in parentheses and a function name of your choosing. For example:

http://api.search.yahoo.com/ ImageSearchService/V1/ imageSearch?appid=YahooDemo &query=Madonna&results=2 &output=json&callback=ws_results

This request results in this output:

ws_results( …json output… );

What is the drawback of this cross-domain solution? Well, I am relying on browsers to automatically execute the JavaScript in any dynamically created script tags. I have yet to find a mainstream browser that doesn’t…but the same goes for XMLHttpRequest. This method is used within Plymouth State University‘s live portal for Savable State Channels and we have yet to receive a support call regarding problems :)

JSON Resources

  • JSON.org: This site is the JSON site. It provides a description of what JSON is, provides some examples, and provides a list of JSON parsers for various languages.
  • Wikipedia: Enough said.
  • JSON Discussion Group
  • Yahoo’s JSON Web Services: Provides examples and suggestions on formatting your REST calls to return JSON as an option


XML works nicely. JSON works just as nicely and faster. This is why I’m a JSON fan. Speed is just as important as the data itself. Without speed…well, you’ve got crap.