Search code examples
apisocrata

Cannot get SODA api to display all data wanted


I'm trying to create a page that collects the MTA turnstile data and tells how many entries there were for each station. I keep getting undefined however, after the first two rows.

var data = {};
        $.getJSON('https://data.ny.gov/resource/ekwu-khcy.json?$$app_token=USrYOYJhmoGZtTFUpKAVu8Sqf', function (json) {
            for(var i = 0; i < json.length; i++) {
                var obj = json[i];
                if (data.hasOwnProperty(obj.station) === false) {
                    data[obj.station] = 0;
                } else {
                    data[obj.station]++;
                }
                
            }
            console.log(data);
            var keys = Object.keys( data );
            for(var i = 0; i < Object.keys(obj).length; i++) {
                $( "div" ).append( "<p><b>"+keys[i]+"</b>: "+data[keys[i]]+" entries </p>");
            }
    });

An image of the page is here.

I'm relatively new at this so I'm pretty sure I did something dumb.


Solution

  • var data = {};
            $.getJSON('https://data.ny.gov/resource/ekwu-khcy.json?$$app_token=USrYOYJhmoGZtTFUpKAVu8Sqf&$select=station,count(*)&$group=station', function (json) {
    
                for(var i = 0; i < json.length; i++) {
                    $( "div" ).append( "<p><b>"+json[i]['station']+"</b>: "+json[i]['count']+" entries </p>");
                }
        });