Click here to Skip to main content
15,922,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to populate an html table of JSON data. I was provided the JSON data and cannot determine why it isn't populating but suspect the JSON may not be formatted properly. When I use other sample data it works, then when I sub in the JSON I was provided it doesn't work. I've tried copying the JSON into a file on my direct server, linking to what I was provided (here: ) [^] and inserting it on myjson.com and reformatting the JSON data.

Here is my code:

HTML
<script>

$(function() {

var entries = [];
var dmJSON = "https://api.myjson.com/bins/6sjud?callback=?";
$.getJSON( dmJSON, function(data) {
   $.each(data.entries, function(i, f) {
      var tblRow = "<tr>" + "<td>" + f.rank + "</td>" + "<td>" + f.name + "</td>" + "<td>" + f.march_rank + "</td>" + "<td> " + f.april_rank + "</td>" +  "<td>" + f.may_rank + "</td>" + f.personal_volume + "</td>" + f.team_volume + "</td>" + "</tr>"
       $(tblRow).appendTo("#incentive tbody");
 });

});

});
</script>


<div class="wrapper">
<div class="profile">
<table id= "incentive" border="1">
<thead>
        <th>Rank</th>
        <th>Name</th>
        <th>March</th>
        <th>April</th>
        <th>May</th>
        <th>Highest Rank</th>
        <th>Personal Volume</th>
        <th>Team Volume</th>
    </thead>
  <tbody>

   </tbody>
</table>

</div>
</div>


Any ideas where I could be going wrong?

What I have tried:

I have tried reformatting the JSON and putting it into a new file on my own server to no avail.
Posted
Updated 1-Mar-18 6:27am

The JSON is perfectly well-formed:
{
  "affiliate": [
    {
      "rank": 1,
      "name": "Sally",
      "march_rank": "Gold",
      "april_rank": "Silver",
      "may_rank": "Silver",
      "highest_rank": "Silver",
      "team_volume": 12345
    },
    {
      "rank": 2,
      "name": "Zelda",
      "march_rank": "Gold",
      "april_rank": "Bronze",
      "may_rank": "Silver",
      "highest_rank": "Gold",
      "team_volume": 12345
    }
  ]
}
The problem is, you're trying to iterate over the entries property, which doesn't exist:
$.each(data.entries, ...

You need to iterate over the affiliate property instead.

You'll need to remove the ?callback=?" query-string, which is confusing jQuery.

You're also missing a couple of opening <td>s, and one column. And there's no personal_volume property in the JSON.

Demo[^]
 
Share this answer
 
I think is the format, it should return [{...}] instead of {...}

Here an example.

CP_JSON_formatting_issue - JSFiddle[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900