Search code examples
javascriptjsondomo

Populating a table with JSON Data


I'm doing something very wrong and I can't for the life of me figure it out. I'm a novice so that doesn't help.

    var res = httprequest.post('https://us3.api.samsara.com/v1/fleet/drivers?access_token=xxxxxxxx','{\"groupId\": 9999}');

DOMO.log('res: ' + res);

var toJson = JSON.parse(res); 


var header = lines[0].split(',');

datagrid.addColumn('id',datagrid.DATA_TYPE_STRING);
datagrid.addColumn('name',datagrid.DATA_TYPE_STRING);

for (var i = 1; i < lines.length; i++) {
  console.warn( lines.[i].id );
  console.warn( lines.[i].name );
}

but the way the data is coming back is all screwey

res: {"drivers":[{"id":12345,"name":"Brian Smith"},
{"id":23456,"name":"Bruce Lee"},
{"id":89234,"name":"Carson Wentz"},

enter image description here


Solution

  • [in progress - i'll get you a working sample just got pulled inot a meeting] first parse it to json.

    var res = '{"drivers":[{"id":12345,"name":"Brian Smith"},{"id":23456,"name":"Bruce Lee"},{"id":89234,"name":"Carson Wentz"}]}';
    
    var toJson = JSON.parse(res); 
    
    // you actually want to be iterating the drivers array
    
    var lines = toJson['drivers'];
    
    for (var i = 1; i < lines.length; i++) {
      console.warn( lines[i].id );
      console.warn( lines[i].name );
    }