-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

The JavaScript JSON Cookbook
By :

JSON stands for JavaScript Object Notation. It's an open standard to represent data as attributes with values. Originally derived from the JavaScript syntax (hence its name) for use in web applications as an alternative to the more verbose and structured Extensible Markup Language (XML), it is now used for data serialization and transport in many standalone and web applications.
JSON provides an ideal means to encapsulate data between the client and server. In this first chapter, you will learn how to work with JSON in languages specified at the beginning of this chapter.
These languages are often used for client-side development, which is what we will focus on here. We'll look more at server-side languages in Chapter 2, Reading and Writing JSON on the Server.
Let's take a look at some JSON returned by the web API, available at http://www.aprs.fi, and modified a bit by me to make the example clear (later, in Chapter 4, Using JSON in AJAX Applications with jQuery and AngularJS, you'll learn how to fetch this data yourself using a web browser and JavaScript):
{ "command":"get", "result":"ok", "what":"loc", "found":2, "entries":[ { "class":"a", "name":"KF6GPE", "type":"l", "time":"1399371514", "lasttime":"1418597513", "lat":37.17667, "lng":-122.14650, "symbol":"\/-", "srccall":"KF6GPE", }, { "class":"a", "name":"KF6GPE-7", "type":"l", "time":"1418591475", "lasttime":"1418591475", "lat":37.17633, "lng":-122.14583, "symbol":"\\K", "srccall":"KF6GPE-7", } ] }
Downloading the example code
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
There are a few things to notice about this example:
KF6GPE
2
or 37.17667
entries
entries
valuetrue
and false
The attribute-name-attribute-value property of JSON, along with the ability to nest values and represent arrays, gives JSON a lot of flexibility. You can represent a lot of common objects using JSON, including most objects that don't have a lot of binary data (For ideas on how to represent binary data using JavaScript and JSON, see Chapter 8, Using JSON for Binary Data Transfer). This includes primitive values (self-documenting because each value is accompanied by an attribute), flat objects with simple values including maps, and arrays of simple or complex objects.
The self-documenting nature of JSON makes it an ideal choice for data transport as you develop new objects, despite its lack of support for comments as you might find in XML. Its plaintext nature makes it amenable to compression over the wire using popular compression schemes such as gzip
(available inside most web servers and web clients), and its format is easier for humans to read than the more verbose XML.
Note that JSON documents are inherently trees, and thus, do not have support for cyclical data structures, such as graphs, where a node points to another node in the same data structure.
If you create such a data structure using the native representation in the programming language you're using and try to convert that to JSON, you'll get an error.
Change the font size
Change margin width
Change background colour