0

I'm trying to learn about basic API calls; I'm having issue with 'dialing in' to specific array objects in this Star Wars API.

I can console.log(json.people) or (json.planets), but cannot seem to go further than this, i.e. console.log(json.people.results) to show me an array of characters and associated data, as this shows as undefined...

I have attempted to parse / stringify the json, but am hitting a wall...

Would anybody be able to point me in the right direction?

Many thanks!

function getData() {

    url = "https://swapi.dev/api/?format=json";
    fetch(url).then((Response) => {
            return Response.json();
        })

        .then((json) => {
            console.log(json.people.results);
            console.log(json.planets);
        });      
}

getData();

1 Answer 1

2

This endpoint returns list of API's. So I take one of those and make another API call. You can skip the first step.

function getData() {

  var url = "https://swapi.dev/api/?format=json";
  fetch(url)
    .then((Response) => Response.json())
    .then((json) => {
      console.log(json)

      var url = json.people;
      fetch(url)
        .then((Response) => Response.json())
        .then((json) => {
          console.log(json)
        });

    });
}

getData();
.as-console-wrapper {
  max-height: 100% !important;
}

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

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.