A Coding Writer’s Guide: API’s and AJAX and JSON ! Oh my!

In between penning the sequel to my novel and code polishing my side projects, I decided it was time to do another concept breakdown on some typical programming terms. Today I will be going over API’s, AJAX, and JSON. Because as a wise person once said: When you learn something new and don’t use it for a while, you inevitably lose it (or so I’m told).

Let’s get started, folks.

API’s or Application Programming Interface as a term was coined by Ira W. Cotton in 1968. The most basic definition of an API is that it is a software intermediary that allows two applications to speak to each other. That’s it! So when you use a social media app, check the daily weather, or send an instant message across the web, you are in fact using an API. Pretty neat, huh? API’s are also commonly used by businesses as a way to integrate functionality across the board of applications in these three ways:

  • Private: The API is only meant for internal company usage.
  • Partner: Specific business partners have access to the API because it allows the companies to exercise quality control by determining which apps are allowed access to the API, which then provides them with an additional revenue stream.
  • Public: The API is openly available for use by the public and the software can be used and written in their platforms.

But wait, there’s more!

Basic infographic on how API’s work

There are thousands upon thousands of API’s out there, but today we will be discussing web API’s, specifically SOAP and REST. Let’s highlight and compare them.

SOAP or Simple Object Access Protocol was designed with official, standardized protocols set from the World Wide Web Consortium. It is function-driven, stateless by default(but can be made stateful), and their API calls cannot be cached. Security supports WS-Security with SSL support and has pre-made ACID compliance. It requires more bandwidth and computing power to use and the messages only come in XML format. SOAP is best used for high-security applications, financial services, payment gateways, enterprise applications, CRM software, and other services that require sensitive information. The main advantage it has over REST is the high security and standardization. However, it does have comparably poorer performance, more complexity/difficulty, and overall less flexibility.

REST or Representational State Transfer on the other hand, is designed in an architectural style with loose guidelines and recommendations. It was created as a response to SOAP and considered more friendly to use. It is data-driven, stateless, and their API calls can be cached. Security supports HTTPS and SSL. It requires less resources than SOAP and the message it sends can come in a variety of formats including: plain text, XML, HTML, JSON, YAML, and more. REST is best used for public API’s in web, mobile, and social networks. It has better performance, responsiveness, scalability, and flexibility compared to it’s SOAP counterpart. However a caveat is there is a lack of security, and is not suitable for distributed environments.

Now that we covered the gist of web API’s, on to the next!

AJAX otherwise known as Asynchronous JavaScript and XML, allows you to update a web page without reloading the page. You can request (by using an XMLHttpRequest object) and also receive data from a server after the page has loaded. You can also send data to the server in the background.

The entire exchange can be summarized in seven steps:

1. An event occurs on a web page (page loads, button is clicked, etc.)

2. An XMLHttpRequest object is created by JavaScript

3. The XMLHttpRequest object sends a request to a web server

4. The server processes the request

5. The server sends a response back to the web page

6. The response is read by JavaScript

7. An action (like page update) is performed by JavaScript

Basic infographic on how AJAX works

Last but not least, we get to some syntax for storing and sending data.

JSON or JavaScript Object Notation originated and was popularized by Douglas Crockford in the early 2000s. If API’s are the conversation between applications, then JSON is considered is the language in which this conversation is transcribed.

It is a widely implemented asynchronous, language-independent format used to send data as it is tailor-made for JavaScript (if having it built into the name wasn’t enough of a sign), it is lightweight, simple to read, and easily applicable with other programming languages. It essentially replaced XML(Extensible Markup Language)for being considered a low-overhead alternative. Since web and mobile applications use API’s prominently in their front ends, performance is a huge factor. This is where JSON shines.

An example of JSON format

As you can see, JSON is represented in key/value pairs in the form of JavaScript Objects. It is written as a property list with each property name as a string enclosed in double quotations, which are then separated by commas, and wrapped in curly braces.

Since the JSON format is text only, it can easily be sent to and from a server, and used as a data format by mostly any programming language. For instance, JavaScript even has a built in function to convert a string, written in JSON format, into native JavaScript objects:

XML itself isn’t terrible, and there still are uses for it depending on the platform. XML is great because it holds any data type and can be used to send a text document with a ton of markup, but it is larger and requires more data to process. JSON was created to be limited and sparse, which is great for business purposes. To put it in perspective, AJAX requests are designed to create a richer user experience without bogging them down with annoying page reloading or frustrating wait times. Responsiveness is the name of the game, especially in a society that constantly needs instant gratification.

This concludes the briefest of summaries on API’s, AJAX, and JSON. Hope it helps all you aspiring coders out there! Stay tuned next time for more!

Founder of Neo Typewriter. He is a published author, podcast host, digital marketer, and full-stack web developer. Read more at: www.andrewjly.com