Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
The JavaScript JSON Cookbook

You're reading from   The JavaScript JSON Cookbook Over 80 recipes to make the most of JSON in your desktop, server, web, and mobile applications

Arrow left icon
Product type Paperback
Published in Jun 2015
Publisher
ISBN-13 9781785286902
Length 192 pages
Edition 1st Edition
Languages
Arrow right icon
Toc

Table of Contents (12) Chapters Close

Preface 1. Reading and Writing JSON on the Client FREE CHAPTER 2. Reading and Writing JSON on the Server 3. Using JSON in Simple AJAX Applications 4. Using JSON in AJAX Applications with jQuery and AngularJS 5. Using JSON with MongoDB 6. Using JSON with CouchDB 7. Using JSON in a Type-safe Manner 8. Using JSON for Binary Data Transfer 9. Querying JSON with JSONPath and LINQ 10. JSON on Mobile Platforms Index

Introduction

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",
    }
  ]
}

Tip

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:

  • The data is organized into attributes and values, each separated by a colon. (Note that a JSON document can also be a single value, such as a string, float, integer, or Boolean value.)
  • Attributes appear as character strings enclosed by double quotes on the left-hand side of a colon.
  • Values are on the right side of the colon and can be the following:
    • Character strings (enclosed in double quotes) such as KF6GPE
    • Numbers (either integers or floating point) such as 2 or 37.17667
    • Arrays (comma-delimited values contained in square brackets), such as the value for entries
    • Whole objects consisting of more attributes and values, such as the two-array values in the entries value
    • Alternatively (although this example doesn't show it), the Boolean values true and false
  • Note that many other kinds of values, such as date/time pairs or individual characters are not supported by JSON.
  • Although it's not entirely clear from this example, whitespace is insignificant. There's no need to have each pair on its own line, for example, and the indentation is completely arbitrary.

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.

Tip

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.

You have been reading a chapter from
The JavaScript JSON Cookbook
Published in: Jun 2015
Publisher:
ISBN-13: 9781785286902
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image