ckportfolio.com - Web Development: Web API, XML and JSON

Web Development: Web API, XML and JSON

Web API

As the method to communicate between application developed by different parties, API (application programming interface) allows the involved stakeholders to exchange data using a common language, including the following:

  • Latest tweets from Twitter
  • YouTube search
  • Google Maps
  • Toronto Open Data

While there is no universally agreed upon approach to building and sharing APIs, there are two main languages in use: XML and JSON.

XML

Similar to HTML in syntax, XML (Extensible Markup Language) is a language designed to store and exchange information in an organized fashion. With focus on simplicity, generality, and usability on Internet, XML is often used to archive news articles, podcasts, and other types of published texts, in the following format:

<books>
    <book>
        <title>Sense and Sensibility</title>
        <author>Jane Austen</author>
        <year>1811</year>
    </book>
    <book>
        <title>Pride and Prejudice</title>
        <author>Jane Austen</author>
        <year>1813</year>
    </book>
    ...
</books>

JSON

As a newer alternative to XML, JSON (JavaScript Object Notation) is a language designed to perform the same function based on JavaScript. With the rising popularity of JavaScript and AJAX-based applications, however, JSON as recently risen as a more popular choice.

<employees>
    <employee>
        <firstName>John</firstName>
        <lastName>Doe</lastName>
    </employee>
    <employee>
        <firstName>Anna</firstName>
        <lastName>Smith</lastName>
    </employee>
    <employee>
        <firstName>Peter</firstName>
        <lastName>Jones</lastName>
    </employee>
</employees>

The above XML document can be rewritten in JSON:

{
    "employees": [
        {
            "firstName":"John",
            "lastName":"Doe"
        },
        {
            "firstName":"Anna", 
            "lastName":"Smith" 
        },
        {
            "firstName":"Peter", 
            "lastName":"Jones"
        }
    ]
}

JSONP

Originally discovered as a security flaw, JSONP (JSON with Padding) is a variant of the original JSON that allows for inter-domain communication, circumventing Same Origin Policy. After a series of iterations, JSONP has earned its place as a de facto standard in Web APIs.

randomPaddingText(
    {
        "Name": "Foo",
        "Id": 1234,
        "Rank": 7
    }
);
Fin