JSON with an undefined or null value

Trying to access an API that contains a specific tree structure:

{"19777621": [{
   "queue": "RANKED_SOLO_5x5",
   "name": "Vladimir's Maulers",
   "entries": [{
      "leaguePoints": 0,
      "isFreshBlood": false,
      "isHotStreak": true,
      "division": "I",
      "isInactive": false,
      "isVeteran": false,
      "losses": 34,
      "playerOrTeamName": "Razdiel",
      "playerOrTeamId": "19777621",
      "wins": 36
   }],
   "tier": "PLATINUM"
}]}

Managed to work with several examples, but struggling with this particular one. The response body appears as undefined or blank when attempting any action.

<head>
    <script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
    <script src="/js/json2.js"></script>
    <script src="/js/json_parse.js"></script>
</head>
<body>
<script>
$.ajax({
url: 'https://euw.api.pvp.net/api/lol/euw/v2.5/league/by-summoner/19777621/entry?api_key=b05c2777-462b-4bcc-ac2a-a3223bb74876',
type: 'GET',
dataType: 'json',
data: {

},
success: function (json) {
document.write("The Result Is:")

    JSON_Encoded = json;
    JSON_Decoded = JSON.stringify(json);
    document.write(JSON_Decoded[19777621].name[0])
    document.write(JSON_Decoded[19777621].entries.losses[0])

},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("error getting Summoner data!");
}
});

</script>

An understanding that my approach is flawed and seeking guidance on how to correct it.

Answer №1

One possible approach is to write it like this...

$.ajax({
url: 'https://euw.api.pvp.net/api/lol/euw/v2.5/league/by-summoner/19777621/entry?api_key=b05c2777-462b-4bcc-ac2a-a3223bb74876',
type: 'GET',
dataType: 'json',
data: {

},
success: function (json){
  document.write("Here is the outcome:")

  //JSON_Encoded = json;
  //JSON_Decoded = JSON.stringify(json);   
  document.write(json['19777621'][0].name)
  document.write(json['19777621'][0].entries[0].losses)

},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("There was an issue fetching Summoner data!");
}
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What do you mean by JSON data?

Is there a way to retrieve data from the database table using JSON and offset 2, where the date matches today's date (e.g. 2011/10/30 = date now)? When I tried to execute this query, the number of rows returned was 0. How can I resolve this issue? Th ...

Function exported as default in Typescript

My current version of TypeScript is 1.6.2 and we compile it to ECMA 5. I am a beginner in TypeScript, so please bear with me. These are the imported library typings. The contents of redux-thunk.d.ts: declare module "redux-thunk" { import { Middle ...

Retrieving innerHTML from a VueJS custom component

Attempting to create a basic bootstrap alert component, I am struggling to access the innerHTML of my custom component. Here is the code snippet: Vue.component('my-alert', { template: '#vue-alert', props: ['type', ' ...

DOM not getting updated by ng-repeat when dynamically inserting elements

Utilizing jsonForm to create a dynamic form using 3 different JSON schemas. Each form submission triggers the display of the next form until all 3 forms are filled in. The data from all 3 forms is then combined into one JSON object, which is sent back and ...

Having trouble with the "Cannot POST /public/ error" when converting a jQuery ajax call to vanilla JavaScript

Following up on a question from yesterday (Update HTML input value in node.js without changing pages), my goal is to submit an HTML form, send an ajax request to the server for two numbers, perform an addition operation on the server, and display the resul ...

Issue with Logstash version 1.5.3 when trying to index JSON data containing a float instead of an integer

I've encountered a strange issue with Kafka and Logstash configuration when using Elasticsearch as an output. I can successfully send a JSON object like this: { "user": "foo" "amount": 1 } But if I try to write: { "user": "foo" "amount": 0.1 ...

Tips for sending multiple variables in a loop as JSON data through an AJAX request

I need assistance with the code below. I am trying to pass the value[i] into a data string in a json ajax post submit. Essentially, my goal is to gather all the checked values in a form and insert them into an existing json data string using ajax. This is ...

NLog: recording a JSON-serialized object

Currently, I am working on a .Net project with NLog configuration that generates JSON-formatted log files. Everything works perfectly with simple text messages. However, I am now faced with the challenge of logging multiple arbitrary objects that are alrea ...

Obtaining Data from Fetch Response Instance

Currently, I am utilizing the fetch method to execute API requests. While everything is functioning as expected, I have encountered a challenge with one specific API call due to the fact that it returns a string instead of an object. Normally, the API pro ...

Is it possible to delete browsing history in Express using node.js?

Upon user login, I store user information in browser sessions on the client side (using Angular) like this: $window.sessionStorage.setItem('loggedInUser', JSON.stringify(val)); For logout authentication on the backend (using Passportjs), I have ...

Is it possible that paramquery is failing to load a JSON string from the servlet URL?

I'm having trouble getting the index.xhtml file to load and display data from a URL. The servlet is functioning correctly, returning the JSON string as shown below: {"data":[{"LASTNAME":"Leonard","PERSON_ID":"0","FIRSTNAME":"Erick","FULLNAME":"Erick ...

Submitting a nested JSON body in ASP MVC

I'm currently struggling to send a nested JSON body to an API, and I've tried a few different approaches without success. The JSON values are sourced from multiple models, making it quite complex for me to handle. Any assistance or guidance on th ...

Utilizing JavaScript in AJAX Responses

Can I include JavaScript in an AJAX response and run it, or should I only use JSON or plain HTML for a more elegant solution? I'm trying to figure out the best way to handle AJAX requests that involve inserting HTML or running JavaScript based on user ...

Why do we recreate API responses using JEST?

I've been diving into JavaScript testing and have come across some confusion when it comes to mocking API calls. Most tutorials I've seen demonstrate mocking API calls for unit or integration testing, like this one: https://jestjs.io/docs/en/tuto ...

Creating unique styles for components based on props in styled MUI: A comprehensive guide

One challenge I am facing is customizing the appearance of my component based on props, such as the "variant" prop using the 'styled' function. Here is an example code snippet: import { styled } from '@mui/material/styles'; const Remov ...

Exploring the functionality of Next.js with Links and routes

Currently, I am facing an issue with the popover menu in my header that displays products. The problem arises when I click on a product in the list; it navigates correctly to the path "products/some-product" regardless of the selected item. However, if I a ...

JavaScript & PHP syntax

Trying to pass PHP into a JavaScript variable. I attempted using the div method with the following code snippet: <div class="service-container" data-service="<?php echo bp_loggedin_user_domain() . BP_XPROFILE_SLUG . '/change-avatar/'; ...

Tricky traversal of a sophisticated SimpleXMLElement structure

I am looking to extract and save certain values from XML data. First step - I begin by obtaining the XML structure: $xml = $dom_xml->saveXML(); $xml_ = new \SimpleXMLElement($xml); dd($xml_); Within this structure, the TextFrame element conta ...

React encountered an abrupt end of JSON input unexpectedly

As I embark on my coding journey, I am delving into the world of React. However, as I try to create a new app, I encounter an error message stating "unexpected end of JSON input." While installing packages and waiting patiently, the console throws an err ...

How can a regular number be used to create an instance of BigInteger in jsbn.js?

I attempted both methods: const num1 = new BigNumber(5); And const num2 = new BigNumber(7, 10); However, I encountered the following error: Error: 'undefined' is not an object (evaluating 'num2.nextBytes') bnpFromNumberjsbn2.js:126 ...