Why JSON is Important to You!

WHAT IS JSON?

JSON stands for JavaScript Object Notation. (JSON is pronounced like the name Jason)

It is a data format. (XML is also a data format)

The JSON data format is ideally suited for consumption by a JavaScript program. (In fact, JSON is JavaScript!)

WHY SHOULD I CARE ABOUT JAVASCRIPT?

Because Ajax applications use JavaScript. (And because JavaScript is very cool)

BUT AJAX APPLICATIONS CAN PROCESS DATA FORMATTED AS XML, SO WHY BOTHER WITH JSON?

If the data is formatted as XML then Ajax is limited to fetching data from the same domain (website) that the Ajax application came from.

If the data is formatted as JSON then Ajax can travel across domains. That is, Ajax can access data from anywhere. Think of the power of this: a single Ajax application, running in your browser, can reach out and grab data from an unlimited number of web services, and present it in your browser in a coherent fashion. Awesome!

WHAT DOES JSON LOOK LIKE?

Here’s information about a person, his website, and his email, all structured as JSON:

{“person”: {
“name”: “John Doe”,
“website”: “http://www.example.com/”,
“email”: “jdoe@example.com”
}
}

HOW IS THE DATA IN JSON EXTRACTED?

Very easy: (suppose the variable “data” holds the above JSON data)

data.person.name returns “John Doe”
data.person.website returns http://www.example.com
data.person.email returns jdoe@example.com

ARE THERE WEB SERVICES THAT SERVE UP JSON?

Yes. For example, all of Yahoo’s Web Services can return JSON if it’s requested. Here’s an example:

http://api.search.yahoo.com/NewsSearchService/V1/newsSearch?appid=xyz&query=Obama&output=json

SHOULD MY WEB SERVICE SERVE UP JSON?

I think that it would be good for your web service to be capable of serving up both XML and JSON. You might implement it in the same way Yahoo does, e.g.

http://www.example.com/web-service?output=json

http://www.example.com/web-service?output=xml

WHO DISCOVERED JSON?

JSON is the brainchild of Douglas Crockford, one of the preeminent JavaScript coders in the world today (http://www.crockford.com).

WHERE CAN I GET MORE INFO?

See the excellent book: Bulletproof Ajax, by Jeremy Keith, p. 77-87

Tags: , , , , , , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: