Analyze the JSON response
JSON is the most common format for responses from REST APIs. Let’s look at the JSON response for the OpenWeatherMap weather endpoint in more depth, distinguishing between arrays and objects in JSON.
- JSON response from OpenWeatherMap weather endpoint
- JSON objects are key-value pairs
- JSON arrays are lists of items
- Including objects in arrays, and arrays in objects
- Examine the weather response
- More information
JSON response from OpenWeatherMap weather endpoint
JSON stands for JavaScript Object Notation. It’s the most common way REST APIs return information. Although some APIs return information in both JSON and XML, if you’re trying to parse through the response and render it on a web page, JSON fits much better into the existing JavaScript + HTML + CSS technology that powers most web pages. With JavaScript, you can easily parse through the JSON and integrate it into your web content.
The unminified response from the OpenWeatherMap weather endpoint looks like this:
{
"coord": {
"lon": -121.96,
"lat": 37.35
},
"weather": [
{
"id": 801,
"main": "Clouds",
"description": "few clouds",
"icon": "02d"
}
],
"base": "stations",
"main": {
"temp": 70.14,
"pressure": 1012,
"humidity": 33,
"temp_min": 62.6,
"temp_max": 75.2
},
"visibility": 16093,
"wind": {
"speed": 14.99,
"deg": 330
},
"clouds": {
"all": 20
},
"dt": 1522619760,
"sys": {
"type": 1,
"id": 479,
"message": 0.0058,
"country": "US",
"sunrise": 1522590707,
"sunset": 1522636288
},
"id": 420006397,
"name": "Santa Clara",
"cod": 200
}
We’ll analyze the information structures within JSON responses in the following sections.
JSON objects are key-value pairs
JSON has two types of basic structures: objects and arrays. An object is a collection of key-value pairs, surrounded by curly braces:
{
"key1": "value1",
"key2": "value2"
}
The key-value pairs are each put into double quotation marks when both are strings. If the value is an integer (a whole number) or Boolean (true or false value), omit the quotation marks around the value. Each key-value pair is separated from the next by a comma.