skip to Main Content

The problem I am getting is I need to get the ID from a API response. The information is in a weird format I have not worked with before so I don’t know how I would get the ID data from this response.

(This is my first question so I am sorry if it is not very clear, feel free to leave comments on how I can fix it)

I am interacting with a API and the response data is:

{
  data: [
    {
      requestedUsername: 'username',
      hasVerifiedBadge: false,
      id: 7838473284,
      name: 'username',
      displayName: 'username'
    }
  ]
}

I tried using .data but then I would get this result:

[
  {
    requestedUsername: 'username',
    hasVerifiedBadge: false,
    name: 'username',
    displayName: 'username'
  }

I am trying to get the data for id though and can’t find a way to get it

My current code:

const userid = await axios({
  method: 'post',
  url: 'https://users.roblox.com/v1/usernames/users',
  data: {
    "usernames": [
      username
    ],
    "excludeBannedUsers": true
  },
})
console.log(userid.data.id)

2

Answers


  1. The data is an array, so it would be:

    const response = await axios({
      method: 'post',
      url: 'https://users.roblox.com/v1/usernames/users',
      data: {
        "usernames": [
          username
        ],
        "excludeBannedUsers": true
      },
    })
    
    console.log(response.data[0].id)
    
    Login or Signup to reply.
  2. The data field in the response is an array, and you need to access the first element of this array to get information about the user. Since you want the id field of the user, you can do this:

    const response = await axios({
      method: 'post',
      url: 'https://users.roblox.com/v1/usernames/users',
      data: { "usernames": [username], "excludeBannedUsers": true },
    });
    
    const userData = response.data.data[0]; // Access the first element of the 'data' array
    const userId = userData.id; // Access the 'id' field of the user object
    console.log(userId);
    

    In this example, I’ve separated the steps to make it clearer, but you can also access the id field directly like this:

    console.log(response.data.data[0].id);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search