0

I can not get the title in JavaScript from my API. I want to display the title in HTML.

api:(https://k0wa1un85b.execute-api.us-east-1.amazonaws.com/production/books)

JSON response looks like:

{"statusCode":200,"body":[{"id":"4f160b1f8693cdbeb96ac8be135ff0f9","title":"Harry Potter"}]}

javascript:

  <body>    
    <p>
        title: <span id="tit"></span><br/>
    </p>    
    <script>
        const api_url = 'https://k0wa1un85b.execute-api.us-east-1.amazonaws.com/production/books';
        async function getISS() {
            const response = await fetch(api_url);
            const data = await response.json();

             console.log(data);

            const {title} = (data);

            document.getElementById('tit').textContent = title;
        }
        getISS();
    </script>
  </body 
1
  • const {title} = data.body[0]; Commented Dec 19, 2019 at 5:45

4 Answers 4

3

Use it like this.

const api_url = 'https://k0wa1un85b.execute-api.us-east-1.amazonaws.com/production/books';
        async function getISS() {
            const response = await fetch(api_url);
            const data = await response.json();

             console.log(data);

            const title = data.body[0].title;
            console.log(title)
            document.getElementById('tit').textContent = title;
        }
 getISS();
<p>
   title: <span id="tit"></span><br/>
</p>    

Sign up to request clarification or add additional context in comments.

Comments

1

Use dot notation to get data from json

async function getISS() {
      const response = await fetch(api_url);
      const data = await response.json();

      console.log(data);

      const title = data[0].title;

      document.getElementById('tit').textContent = title;
}

Comments

1

As suggested by other answers, you can get the value using the array index like this:

var response = {"statusCode":200,
"body":[{
"id":"4f160b1f8693cdbeb96ac8be135ff0f9",
"title":"Harry Potter"}]};
                
console.log(response.body[0].title);

var emptyResponse = {"statusCode":200,
"body":[]};
                
console.log(emptyResponse.body[0].title);

I just want to mention to include the null check otherwise it will through exception:

var response = {"statusCode":200,
"body":[{
"id":"4f160b1f8693cdbeb96ac8be135ff0f9",
"title":"Harry Potter"}]};
                
if(response.body.length){console.log(response.body[0].title);}

var emptyResponse = {"statusCode":200,
"body":[]};
                
if(emptyResponse.body.length){console.log(emptyResponse.body[0].title);}

Comments

0

try this

const api_url = 'https://k0wa1un85b.execute-api.us-east-1.amazonaws.com/production/books';
async function getISS() {
    const response = await fetch(api_url);
    const data = await response.json();
    const title = data.body[0]['title'];
    document.getElementById('tit').textContent = title;
}
getISS();
<p>
    title: <span id="tit"></span><br/>
</p>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.