1

JSON to HTML Table JavaScript Not Working

I've written this function to display the contents of the JSON file in an HTML table. However, it's returning as undefined.

I don't seem to be able to work out why this is happening. The console log displays the data just fine, but not the HTML table.

I'm wondering if it has something to do with the way arrays are parsed?

I want to keep the code short and clean, but wondering if there is a better way to do this without the undefined error. Maybe Vanilla JavaScript is better, using fetch?

Live page is found here: LBRYnomics

Thanks in advance!

jQuery(document).ready(function($) {     $.getJSON('https://www.brendonbrewer.com/lbrynomics/subscriber_counts.json', function(data) {
    var humanTimeSub = `${data.human_time_utc}`
    $(".human-time-sub").html(humanTimeSub)
    var sub_data = '';
    $.each(data, function(key, value) {
      sub_data += '<tr>';
      sub_data += '<td>' + value.ranks + '</td>';
      sub_data += '<td>' + value.vanity_names + '</td>';
      sub_data += '<td>' + value.claim_ids + '</td>';
      sub_data += '<td>' + value.subscribers + '</td>';
      sub_data += '</tr>';
      console.log(key + '=' + value);
    });
    $('#sub-stats').append(sub_data);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="sub-stats">
  <tr>
    <th>RANK</th>
    <th>VANITY NAME</th>
    <th>CLAIM ID</th>
    <th>SUBS</th>
  </tr>
</table>

1
  • Each of those properties ranks & vanity_names & claim_ids etc are individual arrays and are properties of data object itself. You are misinterpreting the data structure received. Do you control the api data source? Commented Sep 10, 2019 at 0:40

2 Answers 2

2

You have multiple different arrays for each of those properties.

Assuming they are all in same relationship order you can loop over one of the arrays and use the iteration index to access corresponding elements from each of the other arrays.

Something like:

jQuery(document).ready(function($) {     $.getJSON('https://www.brendonbrewer.com/lbrynomics/subscriber_counts.json', function(data) {
    var humanTimeSub = `${data.human_time_utc}`
    $(".human-time-sub").html(humanTimeSub);        
    
    var sub_data = '';
    $.each(data.ranks, function(i, rank) {
      sub_data += '<tr>';
      sub_data += '<td>' + rank + '</td>';
      sub_data += '<td>' + data.vanity_names[i] + '</td>';
      sub_data += '<td>' + data.claim_ids[i] + '</td>';
      sub_data += '<td>' + data.subscribers[i] + '</td>';
      sub_data += '</tr>';
      //console.log(key + '=' + value);
    });
    $('#sub-stats').append(sub_data);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="sub-stats">
  <tr>
    <th>RANK</th>
    <th>VANITY NAME</th>
    <th>CLAIM ID</th>
    <th>SUBS</th>
  </tr>
</table>

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

1 Comment

Thanks. This works perfectly and it's just a small amendment. Many thanks.
2

I think after inspecting the api it looks the items are put in different array with no connection but they have the same length. So assuming that attrbute1 at index N maps to attribute2 at index N and so on.

jQuery(document).ready(function($) {     $.getJSON('https://www.brendonbrewer.com/lbrynomics/subscriber_counts.json', function(data) {
    var humanTimeSub = `${data.human_time_utc}`
    $(".human-time-sub").html(humanTimeSub)
    var sub_data = '';
    for(var i=0; i<data.ranks.length; i++){
    //$.each(data.ranks, function(key, value) {
      sub_data += '<tr>';
      sub_data += '<td>' + data.ranks[i] + '</td>';
      sub_data += '<td>' + data.vanity_names[i] + '</td>';
      sub_data += '<td>' + data.claim_ids[i] + '</td>';
      sub_data += '<td>' + data.subscribers[i] + '</td>';
      sub_data += '</tr>';
      //console.log(key + '=' + value);
    //});
    }
    $('#sub-stats').append(sub_data);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="sub-stats">
  <tr>
    <th>RANK</th>
    <th>VANITY NAME</th>
    <th>CLAIM ID</th>
    <th>SUBS</th>
  </tr>
</table>

1 Comment

Thanks. Wasn't sure if I needed the extra for statement, so chose the other answer because it was smaller/cleaner. Many thanks for your help also.

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.