Home > Enterprise >  How to only get certain data from API JSON response data
How to only get certain data from API JSON response data

Time:06-23

I am very new to JS and JSON, I've looked for a while and I can't seem to find the answer. I'm trying to use this API that returns two properties, as seen in the code block below,

{
"quote": "Innovation distinguishes between a leader and a follower.",
"name": "Steve Jobs"
}

It only returns those 2 values and they are randomized each time. Using JS, how do I only grab the "quote" element each time. Thanks in advance!

CodePudding user response:

result['quote']

example

results = {
"quote": "Innovation distinguishes between a leader and a follower.",
"name": "Steve Jobs"
}

console.log(results['quote'])

output

Innovation distinguishes between a leader and a follower.

Edit:

Now I won't be able to actually get the data from your website due to CORS

but you could use the fetch method for it

example

document.onLoad(fetch("https://michael-scott-quotes-api.herokuapp.com/randomQuote")
.then((response)=>{
    data = response.json();
    console.log(data)
})
)

and then you can use the above method to get the quote from it

this will on document load, fetch the data from API and then you can do what you want with it

  • Related