Search results

Access and print a specific JSON value

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

I'm giving an API documentation workshop in Mountain View, California on August 30, 2019. If you're interested, you can register on EventBrite.

This tutorial continues from the previous topic, Inspect the JSON from the response payload. In the sample page where you logged the weather response to the JS Console, the REST response information didn’t appear on the page. It only appeared in the JS Console. You need to use dot notation and JavaScript to access the JSON values you want. In this tutorial, you’ll use JavaScript to print some of the response to the page.

Note that this section will use a little bit of JavaScript. Depending on your role, you might not use this code much in your documentation, but it’s important to know anyway.

Getting a specific property from a JSON response object

JSON wouldn’t be very useful if you always had to print out the entire response. Instead, you select the exact property you want and pull that out through dot notation. The dot (.) after response (the name of the JSON payload, as defined arbitrarily in the jQuery AJAX function) is how you access the values you want from the JSON object.

Let’s say you wanted to pull out the wind speed part of the JSON response. Here’s the dot notation you would use:

response.wind.speed

To pull out the wind speed element from the JSON response and print it to the JavaScript Console, add this to your code sample (which you created in the previous tutorial), right below the console.log(response) line:

console.log("wind speed: " + response.wind.speed);

Your code should look like this:

$.ajax(settings).done(function (response) {
  console.log(response);
  console.log("wind speed: " + response.wind.speed);
});

Refresh your Chrome browser and see the information that appears in the console:

wind speed: 13.87

Printing a JSON value to the page

Let’s say you wanted to print part of the JSON (the wind speed data) to the page, not just the console. (By “print,” I mean make the value appear on the page, not send it to a printer.) Printing the value involves a little bit of JavaScript (or jQuery to make it easier).

I’m assuming you’re starting with the same code from the previous tutorial. That code looks like this:

<!DOCTYPE html>
<html>
   <head>
      <meta charset="utf-8">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
      <title>Sample Page</title>
      <script>
         var settings = {
           "async": true,
           "crossDomain": true,
           "url": "https://api.openweathermap.org/data/2.5/weather?zip=95050&appid=fd4698c940c6d1da602a70ac34f0b147&units=imperial",
           "method": "GET"
         }

         $.ajax(settings).done(function (response) {
           console.log(response);
         });
      </script>
   </head>
   <body>
      <h1>Sample Page</h1>
   </body>
</html>

To print a specific property from the response to the page,

  1. Add the following inside the ajax function:

    $.ajax(settings).done(function (response) {
    console.log(response);
    
    var content = response.wind.speed;
    $("#windSpeed").append(content);
    
    });
    
  2. Then in the page body (inside the body tags), add the following div tag:

    <body>
      <h1>Sample page</h2>
      <div id="windSpeed">Wind speed: </div>
    </body>
    

    Your code should look as follows:

    <!DOCTYPE html>
    <html>
       <head>
          <meta charset="utf-8">
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
          <title>Sample Page</title>
          <script>
             var settings = {
               "async": true,
               "crossDomain": true,
               "url": "https://api.openweathermap.org/data/2.5/weather?zip=95050&appid=fd4698c940c6d1da602a70ac34f0b147&units=imperial",
               "method": "GET"
             }
    
             $.ajax(settings).done(function (response) {
               console.log(response);
    
               var content = response.wind.speed;
               $("#windSpeed").append(content);
    
             });
          </script>
       </head>
       <body>
          <h1>Sample Page</h1>
          <div id="windSpeed">Wind speed: </div>
       </body>
    </html>
    
  3. Refresh the page and you will see the wind speed printed to the page. Here’s an example with both wind speed and weather conditions.

Here’s what we changed:

Inside the tags of the AJAX done method, we pulled out the value we want into a variable, like this:

var content = response.wind.speed;

Then we added a named element to the body of the page, like this:

<div id="windSpeed">Wind speed: </div>

We used the jQuery append method to append the content variable to the element with the windSpeed ID on the page:

$("#windSpeed").append(content);

This code says to find the element with the ID windSpeed and add the content variable after it.

Get the value from an array

In the previous section, you retrieved a value from a JSON object. Now let’s get a value from an array. Let’s get the main property from the weather array in the response. Here’s what the JSON array looks like:

{
  "weather": [
    {
      "id": 801,
      "main": "Clouds",
      "description": "few clouds",
      "icon": "02d"
    }
  ]
]
}

Remember that brackets signify an array. Inside the weather array is an unnamed object. To get the main element from this array, you would use the following dot notation:

response.weather[0].main

Then you would follow the same pattern as before to print it to the page. While objects allow you to get a specific property, arrays require you to select the position in the list that you want.

Here’s the code from the sample page:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<title>Sample Page</title>

<script>
var settings = {
  "async": true,
  "crossDomain": true,
  "url": "https://api.openweathermap.org/data/2.5/weather?zip=95050&appid=fd4698c940c6d1da602a70ac34f0b147&units=imperial",
  "method": "GET"
}

$.ajax(settings).done(function (response) {
  console.log(response);

  var content = response.wind.speed;
  $("#windSpeed").append(content);

  var currentWeather = response.weather[0].main;
  $("#currentWeather").append(currentWeather);

});
</script>
</head>
<body>
<h1>Sample Page</h1>

<div id="windSpeed">Wind speed: </div>
<div id="currentWeather">Current weather conditions: </div>

</body>
</html>

More exercises

If you’d like to follow some more exercises that involve calling REST APIs, accessing specific values, and printing the values to the page, see the following topics in the Glossary and resources section:

22% Complete

22/120 pages complete. Only 98 more pages to go.

Donate?

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