Successfully build advanced JSON-fueled web applications with this practical, hands-on guide with this book and ebook
(For more resources related to this topic, see here.)
JSON is a format and not a language.
XML messages often tend to be heavy and verbose, and take up a lot of bandwidth while sending the data over a network connection. Once the XML message is retrieved, it has to be loaded into memory to parse it; let us take a look at a students data feed in XML and JSON.
The following is an example in XML:
Let us take a look at the example in JSON:
As we notice, the size of the XML message is bigger when compared to its JSON counterpart, and this is just for two records. A real-time feed will begin with a few thousands and go upwards. Another point to note is the amount of data that has to be generated by the server and then transmitted over the Internet is already big, and XML, as it is verbose, makes it bigger. Given that we are in the age of mobile devices where smart phones and tablets are getting more and more popular by the day, transmitting such large volumes of data on a slower network causes slow page loads, hang ups, and poor user experience, thus driving the users away from the site. JSON has come about to be the preferred Internet data interchange format, to avoid the issues mentioned earlier.
Since JSON is used to transmit serialized data over the Internet, we will need to make a note of its MIME type. A MIME (Multipurpose Internet Mail Extensions) type is an Internet media type, which is a two-part identifier for content that is being transferred over the Internet. The MIME types are passed through the HTTP headers of an HTTP Request and an HTTP Response. The MIME type is the communication of content type between the server and the browser. In general, a MIME type will have two or more parts that give the browser information about the type of data that is being sent either in the HTTP Request or in the HTTP Response. The MIME type for JSON data is application/json. If the MIME type headers are not sent across the browser, it treats the incoming JSON as plain text.
The Hello World program with JSON
Now that we have a basic understanding of JSON, let us work on our Hello World program. This is shown in the screenshot that follows:
The preceding program will alert World onto the screen when it is invoked from a browser. Let us pay close attention to the script between the <script> tags.
JSON keys and values have to be enclosed in double quotes, if either are enclosed in single quotes, we will receive an error.
When we use special characters, hyphens, or spaces in our keys, we have to be careful while accessing them.
This article introduced us to JSON, took us through its history, and its advantages over XML. It focussed on how JSON can be used in web applications for data transfer
Resources for Article:
|Successfully build advanced JSON-fueled web applications with this practical, hands-on guide with this book and ebook|
eBook Price: $17.99
Book Price: $29.99
About the Author :