Access and print a specific JSON value · Documenting REST APIs (2024)

Accessing JSON values through dot notation

You'll notice that in the main content display of the weatherdata code, the REST response information didn't appear. It only appeared in the JavaScript Console. You need to use dot notation to access the JSON values you want.

This section will use a tiny bit of JavaScript. You probably wouldn't use this code very much for documentation, but it's important to know anyway.

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

data.query.results.channel.item.description

The dot (.) after data (the name of the JSON payload) is how you access the values you want from the JSON object. JSON wouldn't be very useful if you had to always print out the entire response. Instead, you select the exact element you want and pull that out through dot notation.

To pull out the description element from the JSON response and display it on the page, add this to your code sample, right below the console.log(response) part:

console.log(data.query.results.channel.item.description);

Your code should look like this:

 .done(function (data) { console.log(data); console.log (data.query.results.channel.item.description); });

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

Access and print a specific JSON value · Documenting REST APIs (1)

Printing a JSON value to the page

Let's say you wanted to print part of the JSON (the description element) to the page. This involves a little bit of JavaScript or jQuery (to make it easier).

  1. Add a named element to the body of your page, like this:

     <div id="weatherDescription"></div>
  2. Inside the tags of your done method, pull out the value you want into a variable, like this:

     var content = "data.query.results.channel.item.description";
  3. Below this (same section) use the jQuery append method to append the variable to the element on your page:

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

    This code says, find the element with the ID weatherDescription and append the content variable to it.

    Your entire code should look as follows:

     <html> <body> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script> var settings = { "async": true, "crossDomain": true, "dataType": "json", "url": "https://simple-weather.p.mashape.com/weatherdata?lat=37.354108&lng=-121.955236", "method": "GET", "headers": { "accept": "application/json", "x-mashape-key": "APIKEY" } } $.ajax(settings) .done(function (response) { console.log(response); var content = response.query.results.channel.item.description; $("#weatherDescription").append(content); }); </script> <div id="weatherDescription"></div> </body> </html>

    Here's the result:

    Access and print a specific JSON value · Documenting REST APIs (2)

    Now change the display to access the wind speed instead.

    Access and print a specific JSON value · Documenting REST APIs (2024)
    Top Articles
    Put auto insurance on hold for long vacation? | CarInsurance.com
    Welcome to RandomActsofKindness.org
    11 beste sites voor Word-labelsjablonen (2024) [GRATIS]
    jazmen00 x & jazmen00 mega| Discover
    Walgreens Pharmqcy
    Davante Adams Wikipedia
    2024 Fantasy Baseball: Week 10 trade values chart and rest-of-season rankings for H2H and Rotisserie leagues
    Apply A Mudpack Crossword
    Oriellys St James Mn
    Hallelu-JaH - Psalm 119 - inleiding
    Https //Advanceautoparts.4Myrebate.com
    Nitti Sanitation Holiday Schedule
    Craigslist Malone New York
    House Of Budz Michigan
    Committees Of Correspondence | Encyclopedia.com
    Der Megatrend Urbanisierung
    CANNABIS ONLINE DISPENSARY Promo Code — $100 Off 2024
    How pharmacies can help
    Carson Municipal Code
    623-250-6295
    Wsop Hunters Club
    Wnem Tv5 Obituaries
    kvoa.com | News 4 Tucson
    Does Hunter Schafer Have A Dick
    Myql Loan Login
    104 Presidential Ct Lafayette La 70503
    FAQ's - KidCheck
    Lindy Kendra Scott Obituary
    Co10 Unr
    Downloahub
    Dailymotion
    Www.craigslist.com Syracuse Ny
    2015 Chevrolet Silverado 1500 for sale - Houston, TX - craigslist
    Maybe Meant To Be Chapter 43
    آدرس جدید بند موویز
    Help with your flower delivery - Don's Florist & Gift Inc.
    Missouri State Highway Patrol Will Utilize Acadis to Improve Curriculum and Testing Management
    Santa Cruz California Craigslist
    ENDOCRINOLOGY-PSR in Lewes, DE for Beebe Healthcare
    Ise-Vm-K9 Eol
    Craigslist Mexicali Cars And Trucks - By Owner
    Craigslist Florida Trucks
    Puretalkusa.com/Amac
    Lacy Soto Mechanic
    Linkbuilding uitbesteden
    Brake Pads - The Best Front and Rear Brake Pads for Cars, Trucks & SUVs | AutoZone
    25100 N 104Th Way
    Quest Diagnostics Mt Morris Appointment
    Understanding & Applying Carroll's Pyramid of Corporate Social Responsibility
    Ingersoll Greenwood Funeral Home Obituaries
    Jasgotgass2
    Latest Posts
    Article information

    Author: Lidia Grady

    Last Updated:

    Views: 6360

    Rating: 4.4 / 5 (65 voted)

    Reviews: 88% of readers found this page helpful

    Author information

    Name: Lidia Grady

    Birthday: 1992-01-22

    Address: Suite 493 356 Dale Fall, New Wanda, RI 52485

    Phone: +29914464387516

    Job: Customer Engineer

    Hobby: Cryptography, Writing, Dowsing, Stand-up comedy, Calligraphy, Web surfing, Ghost hunting

    Introduction: My name is Lidia Grady, I am a thankful, fine, glamorous, lucky, lively, pleasant, shiny person who loves writing and wants to share my knowledge and understanding with you.