Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion


Hello, I need help understanding JSON api's there is a tutorial in the pipeline on treehouse but I sort of need to learn it now.



9 Answers

Ethan Lowry
Ethan Lowry
Courses Plus Student 7,323 Points

Hey. What in particular are you wanting to achieve? Is there a specific service's API you're looking to access? Is this from a mobile or web app, and in what language?

Making use of these API's tends to be quite simple - at a high level, all your code needs is to A) Make a request to the target URL which returns the JSON you need, and B) a way to read the returned JSON and parse it to retrieve whichever data you want, at which point you can use it like any other variable in your app.

I know there are existing tutorials out there on other resources such as Codecademy or Code School which you might look into if you really want a guide.

Dave McFarland
Dave McFarland
Treehouse Teacher

Hi Richard Duffy

APIs can differ a lot between services. Ethan Lowry's correct -- we need to know which API you want to use and what language you will be using to help.

Sam Chaudry
Sam Chaudry
25,519 Points

Hi Richard

As Dave mentioned there are lots of different types of API available on line to get different types of information. But if you want a quick overview I will try and give you a insight quickly without trying to confuse you.

API's in a nutshell is a way of sending information across the wire to your website from a web service. JSON consists of and array of objects that hold this data so for example lets look at this:


This is a JSON open weather API consisting of multiple objects held in an array, to get an idea of whats in it, download JSON viewer for Chrome and you will see the objects as a stack/list. Each of these objects is held at an index, contain another object/objects and its one of these you may be interested in, so lets look the Wind object as an example:

In the JSON view it looks like this:

wind: {
speed: 1.61,
deg: 208.5

This contains speed (1.61) and deg (208.5) properties that you are interested in. Notice that each of these objects is set up in pairs which we call key/value pairs. So Speed is a key and it's value is 1.61. However in this format it's not useful if want the wind speed!

To get access to this you would a AJAX call first to retrieve the data and hold it in a variable called data for example. But to get access to each to each of these objects there are you can run a for loop

For loop: This is the standard way one that I've used many a time to extract the properties, you want and it looks like this below:

for(var i = 0; i < data.length; i ++)

So here you are looping through the length of the array or your list of objects which have been returned to you. To get access to the object you want pause your Javascript in the Chrome debugger and in the console type data. Note that you have to pause on the line which contains the data.

When you type data you will see the output as "object" and click on this it will show you a complete list of data objects. Including the wind object you are interested so to get that you would next in the console put in something like data[0].

Here your saying "show me the object at index an index value" and when you hit return it would show you that. So it could be clouds you want if you type for example data[3], you just substitute in the index number for the object you want. To access the properties of this we would use the (.) syntax and it would look like i.e data[4].speed for example.

For a complete overview in javascript it would be as follows:

var windSpeed="" //Note this is not assigned a value as it waiting for whatever the wind speed property is.

for(var i = 0; i < data.length; i ++){ //Loop through the length of the objects 

windSpeed += data[4].speed. //Find me the object at index 4 which is the wind object and get me it's speed property


(#weather).html( "The wind speed is" + windSpeed); //jQuery to parse it out on the screen

So you have basically looped through the length JSON objects and said "when you find in the length of the data objects an object at index 4 give me its speed and hold it in a variable called windSpeed. Then you take your weather div (#weather) passed a method called html and in the following brackets outputted: The wind speed is (hard coded string) and concatenated the value of windSpeed variable on the end. So in you website in your weather div it would show

The wind speed is 1.61;

The interesting part is that as wind speed object in the API changes the information in your website will also change without the need to hard code in HTML each time. I have used a hypothetical example to demonstrate how this works, but you should give it a go, to make sure that you are equipped with it check out Team Treehouse course on AJAX jQuery and Chrome Dev Tools, these will set you up with the right skills needed to understand this.

Note that you can also use a For each loop to to do the same thing, but if you understand how I've explained it you will find the second way much simpler to understand.

Hope this hasn't lost you totally and is some help!

Dave McFarland
Dave McFarland
Treehouse Teacher

Great explanation Sam Chaudry ! (I updated your response to format the JavaScript using syntax highlighting. If you want to learn how to format code in the forums check out this forum post: https://teamtreehouse.com/forum/posting-code-to-the-forum)

Wow! I am touched by your help! I need to learn the CPANEL api! Thank you.

Dave McFarland
Dave McFarland
Treehouse Teacher

HI Richard Duffy

I didn't even know that cPanel had an API. What exactly are you trying to do? Do you want to build your own cPanel interface? Or are you simply wanting to use cPanel to manage a web server?

I need to create a web hosting account through my cpanel account.

Dave McFarland
Dave McFarland
Treehouse Teacher

You don't need to learn an API then. An API, or Application Programming Interface, is only need if you want to write your OWN administration panel that issues cPanel commands.

To use cPanel, I found this YouTube video: http://www.youtube.com/watch?v=q2Q3Ry4G21M

Who is your web hosting company? Don't they provide documentation?

5wire.co.uk I have looked at the documentation but just do not understand it.


Dave McFarland
Dave McFarland
Treehouse Teacher

Well, you don't need to learn the API. There should be a regular cPanel interface. According to the site's docs you should be able to go to http://cpanel.5wire.co.uk to login.

You should call or email their customer support for more information on how to use cPanel on their server.

It is for a tutorials project I am working on.

No, I think you misunderstand me, I want to be able to create my own form using the api to create accounts from that form that I program using the api.

Dave McFarland
Dave McFarland
Treehouse Teacher

Ah, I see. You DO want to build an admin panel that uses the API. Sorry. In that case, you do need to go through their documentation. My AJAX Basics course will be out next month, but it doesn't address this kind of API call.

Okay, this might be a start with your api's.