All rights reserved. No portion of this book may be reproduced mechanically, electronically, or by any other means, including photocopying without the permission of the publisher
The information provided in this book is designed or written to provide helpful information on the subjects discussed. The authors books are only meant to provide the reader with the basic knowledge of a certain topics related to different subjects, without any warranties whether the student will, or will not, be able to incorporate and apply all the information provided. Although the writer has made his best effort to share the insights of JSON with the help different tutorials in this book but there is need to understand that learning is a difficult task and each person needs a different timeframe to fully incorporate a new topic. Neither this book nor any other book of the writer promises that reader will learn certain topics and subjects at any extent within a certain timeframe. This is all because learning is process that depends various aspects including the learners capability to understand, practice and perform the topic or subject he/she is learning.
Chapter 1: Introduction to JSON
Objective: This chapter introduces you to JSON, helps you understand why its used, and how its used in the development industry.
For many years, XML was the standard format for structured data. It was easy to use and similar to HTML with tags that contained data. XML was the popular format for data being passed between APIs or even database output and input.
Then, JavaScript Object Notation (JSON) was introduced at the beginning of the millennium. In 2005, Yahoo began offering JSON as an output data type, and it quickly became a de facto for data transfers. What makes JSON an interesting option is that its completely platform and language independent. You can use JSON in older systems such as Classic ASP and Java or newer languages such as C#, Ruby, and PHP. The data content is text-based, so its easily read across all platforms, and even humans can read and understand a JSON files content.
XML is still used in some applications, but JSON is quickly overtaking the older format as the standard API communication tool. JSON also works well with Ajax and jQuery, so you can send and receive asynchronous communications. Asynchronous communication is more user friendly and reduces load on the web server. Instead of refreshing the entire web page, you can code your site to send and receive JSON communication with one HTTP request.
Why Use JSON?
You might ask yourself why you should change to JSON from XML if XML works just fine. The problem with XML is that its an extremely bulky and has a lot of overhead to use it. Tags, properties and other bulky data were included in an XML file. For a small data set, the overhead was huge. As a matter of fact, most programmers were unimpressed with XML when it was first introduced. It was difficult to read and difficult to parse through. As it turns out, JSON was a much better option and programmers first reactions were accurate.
JSON is much simpler, has little overhead to use, and it has a format that is easy to read. For instance, if you have an object with a first and last name, you can easily identify what components in the JSON output matches with this input.
There are no properties are markup in JSON, which also makes it much more lightweight. With XML, properties and markup determined a files syntax structure. Any missing tag could throw off any XML parser. JSON also requires proper structure, but its much easier to implement and create.
If youve ever developed with XML, you know that you must build a document piece by piece, and any small structural error throws off your document. It could be easy to identify the issue with smaller documents, but larger XML documents are difficult to troubleshoot. With JSON, you get a textual representation of your data without the bulky tags and markup.
JSON is also universal, so you can use JSON with any language or platform. Since its text based, JSON uses Unicode. This means that an application written for a Spanish audience by an English developer can use JSON without any special internationalization on the code. Tags dont need to be in any specific language the coder just needs to identify each object item. The value can be in any language.
Overall, JSON is widely becoming the standard for data driven applications. There are numerous libraries that handle parsing of the data, so you dont even need much experience with JSON to get started. With just a small amount of understanding, you can work with JSON data without much work on your part. Several libraries handle building JSON objects as well. For instance, passing data to and from a C# controller can be done in one line of code using a specific .NET framework library.
Finally, another great benefit to JSON is that you dont need any special software to work with it. Remember, its a text based format, so you dont need any software to build objects or even read JSON files. Some development environments help you view JSON files by color coding variables and objects. Color coding helps developers better read the content just like it helps them code more easily.
In this eBook, we will show you how to use JSON, and youll see that many of these benefits are illustrated with the code and structure. Once you work with JSON, youll never want to return to XML again.
What is JSON Used For?
For the most part, JSON is used in JavaScript since its a JavaScript object. You can still pass JSON in other languages, but most people use JSON for asynchronous transactions. Youll see a lot of JSON in Ajax and jQuery code. The Ajax or jQuery code passes information asynchronously to the main language such as C# or PHP.
We should note what asynchronous calls mean to a coder and a user. Traditionally, when a user sent data to a web application, the user clicked a Submit button and the browser would send the information to the web server. Once the data was processed, a response is sent back to the users browser and a new page is displayed. All elements of the new web page were retrieved and re-rendered on the users browser including navigation, sidebars, footers and any common content. This was sufficient for older web applications, but users have become more demanding and want faster web responses.
Asynchronous web calls solve the issue of speed and fast responses. Asynchronous web calls only send the form portion of a web page back to the web server. The footer, header, sidebars and any other static components on the page arent sent to the server. The result? A faster response and users dont have to wait for a response. The transaction is faster for the server because it doesnt need to retrieve and process all objects on a web page. It only needs to process the data, which is usually processed on a database. The small amount of processing power needed from the web server leaves resources for other more intensive applications. The result is a better experience for the user. The benefit is less processing power needed from the web server, which results in a cost savings and efficiency for website owners.
Now that you understand asynchronous versus synchronous calls to a web server, you can understand the way JSON works and what its used for. Youve probably come across JSON calls on several websites and dont even realize it.
First, JSON is used to pass data back and forth in a web application. This web application could be an internal application used for a corporate environment, or it could be a public website. Lets take an example of signing in to an application.