Search results

Analyze the JSON response

Keep current with the latest trends in technical communication by subscribing to the I'd Rather Be Writing newsletter. 4,500+ subscribers

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 formatting.

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
}

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.

JSON arrays are lists of items

An array is a list of items, surrounded by brackets:

["first", "second", "third"]

The list of items can contain strings, numbers, booleans, arrays, or other objects. With integers or booleans, you don’t use quotation marks.

Integers:

[1, 2, 3]

Booleans:

[true, false, true]

Including objects in arrays, and arrays in objects

JSON can mix up objects and arrays inside each other. You can have an array of objects:

[
  object,
  object,
  object
]

Here’s an example with values:

[  
   {  
      "name":"Tom",
      "age":39
   },
   {  
      "name":"Shannon",
      "age":37
   }
]

And objects can contain arrays in the value part of the key-value pair:

{
"children": ["Avery","Callie","lucy","Molly"],
"hobbies": ["swimming","biking","drawing","horseplaying"]
}

Just remember, objects are surrounded with curly braces { } and contain key-value pairs. Sometimes those values are arrays. Arrays are lists and are surrounded with square brackets [ ]. It’s common for arrays to contain lists of objects, and for objects to contain arrays.

It’s important to understand the difference between objects and arrays because it determines how you access and display the information. Later exercises with dot notation will require you to understand this difference.

Examine the weather response

Look at the response from the weather endpoint of the OpenWeatherMap weather API. Where are the objects? Where are the arrays? Which objects are nested? Which values are booleans versus strings?

More information

For more information on understanding the structure of JSON, see json.com.

20% Complete

20/110 pages complete. Only 90 more pages to go.

Donate?

Want to buy me lunch? Click the Donate button below to donate $10 through Paypal.