Skip to content

Instantly share code, notes, and snippets.

@t1089
Created March 26, 2020 13:33
Show Gist options
  • Select an option

  • Save t1089/fab28b22313253ab520ce48105afbe04 to your computer and use it in GitHub Desktop.

Select an option

Save t1089/fab28b22313253ab520ce48105afbe04 to your computer and use it in GitHub Desktop.
json table
<h1>Table</h1>
<div class="container">
<table id="results" class="table table-striped">
<!-- dynamic table json -->
</table>
</div>
$.ajax({
url: "https://health.data.ny.gov/resource/h5s5-hcxg.json",
//force to handle it as text
dataType: "text",
success: function(data) {
//data downloaded so we call parseJSON function
//and pass downloaded data
var json = $.parseJSON(data);
//now json variable contains data in json format
//let's display a few items
// we'll put all our html in here for now
var html = '';
for (var i=0;i<json.length;++i)
{
// if on first loop, create the col headers
if(i===0){
html += '<thead class="bg-warning lead text-center"><tr>';
$.each(json[i], function(key, value){
html += '<td>'+key+'</td>' ;
});
html += '</tr></thead>';
}
// loop through all the json objects and for every key add a column with the value
html += '<tr>';
$.each(json[i], function(key, value){
html += '<td>'+value+'</td>' ;
});
html += '</tr>';
}
// push all the html in one go to the page
$('#results').append(html);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment