Home > 2010 Fall DPS907 > JSON introduction for Seneca ICT students

JSON introduction for Seneca ICT students

October 13, 2010 Leave a comment Go to comments

This post is an introduction to JSON, for Seneca School of Information and Communications Technology students. We will build upon your past knowledge and experience in JavaScript.

.

Locating help documentation

Your professor suggests that you study the following JSON documents before continuing:

Seven JavaScript Things I Wish I Knew Much Earlier In My Career blog post, specifically these sections:

  • Shortcut Notations
  • JSON As A Data Format

JSON.org home page

Wikipedia article on JSON

OData documentation, JSON Format

.

JSON introduction

The documentation links (above) do a great job of introducing and describing JSON, so we will not attempt to improve on them here.

However, we can introduce JSON in the context of your recent work in the course. The specifications of Lab 1 required you to consider JSON requests and responses, but you were not required to test Lab 1 using JSON. We’ll now some testing here and below, as a way of actively introducing JSON.

Let’s start with GET requests from the WCF Data Service, because they’re simple. At this point in time, you are familiar with the Northwind entities, so let’s work with some of them.

When a WCF Data Service returns JSON, the JSON is in an object wrapper. It has a single name-value pair. Its name is “d”, and its value is the JSON data. As you learned in the OData documentation on the JSON Format, this is done as a security measure, so that the returned JSON data is treated as data, and not as code.

All of the following examples assume that “foo” has already been declared. The data returned is the object that is delimited by the curly braces.

.

GET data from the service root

The returned JSON data looks like this:

.

GET data from category 17

The returned JSON data looks like this:

.

GET data from the shippers entity collection

The returned JSON data looks like this:

.

Where JSON can be used

JSON is a language-independent text format. Just like XML. Its use is not limited to JavaScript. For example, JSON serialization (and deserialization) is built into the .NET Framework 4. JSON can also be used in many other client-side and server-side technologies and platforms.

However, it is very easy to use in JavaScript. We will be introduced to using JSON in JavaScript, in a blog post about jQuery, to be posted in the near future.

.

Working with JSON in Lab 1

As noted above, your Lab 1 should already be JSON-compatible. GET requests are easy. A DELETE request is also easy, because it does not receive or return data.

That leaves POST and PUT. The OData documentation tells us that both requests require a JSON object, but the object wrapper is not required. The examples below show simple JSON objects that can be used in a POST and PUT. Future examples will discuss and show how to include links/navigation properties (but if you can’t wait, check the OData documentation).

.

POST a new entry/entity to the shippers collection

The following JSON object can be sent in a POST request:

{ “CompanyName” : “DHL”, “Phone” : “(800) CALL-DHL” }

.

PUT to update an existing shippers entry/entity

The following JSON object can be sent in a PUT request, to update shipper #6:

{ “CompanyName” : “DHL Express”, “Phone” : “(800) CALL-DHL” }

The following JSON object can be sent in a PUT request, to update the “CompanyName” property of shipper #6:

{ “CompanyName” : “DHL Express Canada” }

.

Summary

As you have learned, JSON is an easy-to-learn and easy-to-use data format. In the near future, we will build on this foundation, and build a full-featured JavaScript client.

.


.

.

.

Advertisements
Categories: 2010 Fall DPS907
  1. No comments yet.
  1. No trackbacks yet.

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: