averysumner averysumner - 4 months ago 16
Javascript Question

Can't get specific element of API response [JS]

I can't seem to get a specific part out of the response below, using the code that is also below. I get an "undefined" in the console log when I try to use

response["minecraft.net"]
. Can someone explain to me why this isn't working?

Response:

"[{"minecraft.net":"green"},{"session.minecraft.net":"green"},{"account.mojang.com":"green"},{"auth.mojang.com":"green"},{"skins.minecraft.net":"green"},{"authserver.mojang.com":"green"},{"sessionserver.mojang.com":"green"},{"api.mojang.com":"green"},{"textures.minecraft.net":"green"},{"mojang.com":"green"}]"


Code:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Mojang Servives Status</title>
</head>

<body>
<script>
var api = new XMLHttpRequest();
api.open("GET", "https://status.mojang.com/check", false);
api.send();
console.log(api);
var response = JSON.parse(api.response)
console.log(response["minecraft.net"]);
</script>
</body>

</html>

Answer

The response is wrapped inside of an array. So you'd use response[0]['minecraft.net']

Comments