Chapter 35. JSON

In Lessons 33 and 34, you sent a request to the server and received data that you did something with; in Lesson 33, you retrieved a list of names separated by commas and parsed that data to make a functional application. Lesson 34 had you make a POST request to the server, which responded with text that you displayed in an alert box. The data returned from the server in those two lessons was simple, and you will rarely deal with such a simplistic set of data in a real-world application. So this lesson introduces you to an interchange format suitable for representing complex data. It is today's standard in Ajax communication: JavaScript Object Notation (JSON). First, let's take a look at how JSON came about.

XML RULES THE WORLD...OR DOES IT?

Since its creation, eXtensible Markup Language (XML) has steadily gained ground as the data interchange format of choice. The ability to mark up data and exchange it between different computer systems made XML the de facto standard for data transmission. Don't worry if you're not familiar with XML. The basics of the language are very simple. In fact, it looks a lot like HTML. Following is a simple XML document that describes a person:

<?xml version="1.0" encoding="utf-8"?> <person> <firstName>John</firstName> <lastName>Doe</lastName> <address> <street>1234 XYZ</street> <city>Dallas</city> <state>Texas</state> <country>USA</country> </address> <phoneNumbers> <phoneNumber type="home">111-123-4567</phoneNumber> <phoneNumber type="cell">111-234-5678</phoneNumber> ...

Get JavaScript® 24-Hour Trainer now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.