I'm currently learning how to fetch an API in javascript and I'm encountering some difficulties in extracting specific parts of the response body. My goal is to store the setup and punchline of a joke in variables for later use.
Here is the code I have written:
fetch("https://dad-jokes.p.rapidapi.com/random/joke", {
"method": "GET",
"headers": {
"x-rapidapi-host": "dad-jokes.p.rapidapi.com",
"x-rapidapi-key": "79142f2e8cmsh903cd5752a9ee77p1166f8jsnb9c812f77793"
}
})
.then(res => res.json())
.then(data => console.log(data));
This code snippet returns the following response:
{
{
success: true,
body: [
{
_id: "60dd3699212bcedc7b8720a1",
setup: "I saw a poster today for a free concert for those working in public health. It said 'Frontline Only'...",
punchline: "Weird. I would've thought they'd fill the whole venue.",
type: "health",
likes: [],
author: { name: "unknown", id: null },
approved: true,
date: 1618108661,
NSFW: false
}
]
}
My objective is to extract and save the setup and punchline of the joke from the response for individual use. What would be the best approach to achieve this? Thank you.