JSON API Example

For example, lets look at a public JSON API example. There’s not a ton of public APIs, most require authentication, but github has some public data available: https://api.github.com/users/adafruit here’s what you’ll get (of course some dates and numbers may vary)

{
  "login": "adafruit",
  "id": 181069,
  "avatar_url": "https://avatars3.githubusercontent.com/u/181069?v=4",
  "gravatar_id": "",
  "url": "https://api.github.com/users/adafruit",
  "html_url": "https://github.com/adafruit",
  "followers_url": "https://api.github.com/users/adafruit/followers",
  "following_url": "https://api.github.com/users/adafruit/following{/other_user}",
  "gists_url": "https://api.github.com/users/adafruit/gists{/gist_id}",
  "starred_url": "https://api.github.com/users/adafruit/starred{/owner}{/repo}",
  "subscriptions_url": "https://api.github.com/users/adafruit/subscriptions",
  "organizations_url": "https://api.github.com/users/adafruit/orgs",
  "repos_url": "https://api.github.com/users/adafruit/repos",
  "events_url": "https://api.github.com/users/adafruit/events{/privacy}",
  "received_events_url": "https://api.github.com/users/adafruit/received_events",
  "type": "Organization",
  "site_admin": false,
  "name": "Adafruit Industries",
  "company": null,
  "blog": "www.adafruit.com",
  "location": "New york city",
  "email": null,
  "hireable": null,
  "bio": null,
  "public_repos": 838,
  "public_gists": 1,
  "followers": 0,
  "following": 0,
  "created_at": "2010-01-12T23:57:58Z",
  "updated_at": "2017-08-23T10:11:22Z"
}

As you can see, the JSON data is very structured but pretty simple. You can see the location is a string:

"location": "New york city",

“New York City” but the number of public repositories (public_repos)

"public_repos": 838,

is a number 838 and other entries are URLs of their own,

"avatar_url": "https://avatars3.githubusercontent.com/u/181069?v=4",

boolean values,

"site_admin": false,

etc.

Since JSON is text-based you can really toss whatever data you like in there. If you need to store binary data, you’ll need to textify it, using something like Base64 encoding. (Compare this to MQTT where the data is not text based but raw binary and can be parsed however you like)

XML API Example

The URL itself can end up becoming a data transport of its own. For example Yahoo’s weather API has URLs like this:

https://query.yahooapis.com/v1/public/yql?q=select%20item.condition.text%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22NYC%2C%20NY%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys

Note the format=json part of the URL. When you go here you'll get JSON data (we've added line breaks for legibility)

{
  "query": {
    "count": 1,
    "created": "2017-09-05T04:27:15Z",
    "lang": "en-US",
    "results": {
      "channel": {
        "item": {
          "condition": {
            "text": "Clear"
          }
        }
      }
    }
  }
}

If you rewrite the URL to have format=xml you can see the XML output:

<query yahoo:count="1" yahoo:created="2017-09-05T04:28:53Z" yahoo:lang="en-US">
  <results>
    <channel>
      <item>
      <yweather:condition text="Clear"></yweather:condition>
      </item>
    </channel>
  </results>
</query>
<!-- total: 11 -->

You can see the data is the same, but the format is a little different.

This guide was first published on Dec 14, 2017. It was last updated on Dec 14, 2017.

This page (API Example) was last updated on Dec 14, 2017.

Text editor powered by tinymce.