computerguyinhere
computerguyinhere

Reputation: 29

JSON returns only one object

I'm working in Domo pulling users from an API. I'm trying to pull ALL users and included addition entries in the site aka custom fields. As of now, it's only pulling one user. Also, in the obj2 log, it's only returning one object. Since it's not returning all the objects, it's never getting ID 4 which is Department. It only outputs ID 11 which is a type of ID we use and stops.

I feel like I'm close...any pointers what I need to fix?

I'm extremely new to JavaScript, be kind!

if(metadata.report == 'User'){
DOMO.log('metadata.reportname: ' +  metadata.report);
var base_url = 'https://app.com/api/author/users/';
var res = httprequest.get(base_url);
//DOMO.log('res: ' + res);
var raw = JSON.parse(res);
datagrid.addColumn('id', datagrid.DATA_TYPE_STRING);
datagrid.addColumn('hris_id', datagrid.DATA_TYPE_STRING);
datagrid.addColumn('full_name', datagrid.DATA_TYPE_STRING);
datagrid.addColumn('first_name', datagrid.DATA_TYPE_STRING);
datagrid.addColumn('last_name', datagrid.DATA_TYPE_STRING);
datagrid.addColumn('department', datagrid.DATA_TYPE_STRING);

var users = raw.users;
//DOMO.log('users: ' + JSON.stringify(users));

for(var i = 0; i < users.length; i++){
  var obj = users[i];
}
  //make new specific request with includes
  var temp = obj.id;
  var temp2 = '?includes[]=custom_fields';
  var uniq_url = base_url.concat(temp.concat(temp2));
  //DOMO.log(uniq_url);
  var res2 = httprequest.get(uniq_url);
  var raw2 = JSON.parse(res2);
  //DOMO.log((raw2));

  var linked = raw2.linked;
  var cf = linked.custom_fields;
  var cfv = linked.custom_field_values;

 for(var g = 0; g < cfv.length; j++){
    var obj2 = cfv[g];
    DOMO.log(obj2.value);
  }
    if (obj2.custom_fields.id == 4){ 
    DOMO.log("Department is: " + obj2.value);
    }

    //if (obj2.links.custom_field.id == 5){
      //DOMO.log('Position is ' + obj2.value);


  //DOMO.log('JSON ' + res);
  //DOMO.log('id: ' + obj.id);
  datagrid.addCell(obj.id);
  //DOMO.log('hris_id: ' + obj.hris_id);
  datagrid.addCell(obj.hris_id);   
  //DOMO.log('full_name: ' + obj.hris_id);
  datagrid.addCell(obj.full_name);          
  //DOMO.log('first_name: ' + obj.first_name);
  datagrid.addCell(obj.first_name);
  //DOMO.log('last_name: ' + obj.last_name)
  datagrid.addCell(obj.last_name);
  //DOMO.log('value: ' + obj.value);
  datagrid.addCell(obj.value);
  datagrid.endRow();
}  

//Console Log;
metadata.reportname: User
https://app.com/api/author/users/976?includes[]=custom_fields
{"id":"3785","value":"12870","links":{"custom_field":{"id":"11","type":"custom_fields"}}}

Upvotes: 2

Views: 131

Answers (1)

Ziv Weissman
Ziv Weissman

Reputation: 4536

The problem is with the for loop:

for(var i = 0; i < users.length; i++){
  var obj = users[i];
}

You are iterating all users, but always taking the current [i] user.

I'm guessing that you meant that bracket will surround the entire code:

Like this:

var users = raw.users;
//DOMO.log('users: ' + JSON.stringify(users));
for (var i = 0; i < users.length; i++) {
  var obj = users[i];

  //make new specific request with includes
  var temp = obj.id;
  var temp2 = '?includes[]=custom_fields';
  var uniq_url = base_url.concat(temp.concat(temp2));
  //DOMO.log(uniq_url);
  var res2 = httprequest.get(uniq_url);
  var raw2 = JSON.parse(res2);
  //DOMO.log((raw2));

  var linked = raw2.linked;
  var cf = linked.custom_fields;
  var cfv = linked.custom_field_values;

  for (var g = 0; g < cfv.length; j++) {
    var obj2 = cfv[g];
    DOMO.log(obj2.value);
  }
  if (obj2.custom_fields.id == 4) {
    DOMO.log("Department is: " + obj2.value);
  }
}

Edit: After your additional input, I see another mistake in the next for loop - same issue, you are iterating it but eventually taking one value (the last one) and there is a mistake because j is not defined, because you are using g.

Fixed code:

  if (metadata.report == 'User') {
    DOMO.log('metadata.reportname: ' + metadata.report);
    var base_url = 'https://app.com/api/author/users/';
    var res = httprequest.get(base_url);
    //DOMO.log('res: ' + res);
    var raw = JSON.parse(res);
    datagrid.addColumn('id', datagrid.DATA_TYPE_STRING);
    datagrid.addColumn('hris_id', datagrid.DATA_TYPE_STRING);
    datagrid.addColumn('full_name', datagrid.DATA_TYPE_STRING);
    datagrid.addColumn('first_name', datagrid.DATA_TYPE_STRING);
    datagrid.addColumn('last_name', datagrid.DATA_TYPE_STRING);
    datagrid.addColumn('department', datagrid.DATA_TYPE_STRING);

    var users = raw.users;
    //DOMO.log('users: ' + JSON.stringify(users));

    for (var i = 0; i < users.length; i++) {
      var obj = users[i];
      //Move brackets to end

      //make new specific request with includes
      var temp = obj.id;
      var temp2 = '?includes[]=custom_fields';
      var uniq_url = base_url.concat(temp.concat(temp2));
      //DOMO.log(uniq_url);
      var res2 = httprequest.get(uniq_url);
      var raw2 = JSON.parse(res2);
      //DOMO.log((raw2));

      var linked = raw2.linked;
      var cf = linked.custom_fields;
      var cfv = linked.custom_field_values;

      //for (var g = 0; g < cfv.length; j++) { --> g++ instead of j++
      for (var g = 0; g < cfv.length; g++) {
        var obj2 = cfv[g];
        DOMO.log(obj2.value);
        //Move bracket to after using obj2
        //Added more conditions:
        if (obj2.custom_fields.id == 4 || obj2.custom_fields.id==5 || obj2.someOtherProp=="Something") {
          DOMO.log("Department is: " + obj2.value);
          //DOMO.log('JSON ' + res);
          //DOMO.log('id: ' + obj.id);
          datagrid.addCell(obj.id);
          //DOMO.log('hris_id: ' + obj.hris_id);
          datagrid.addCell(obj.hris_id);
          //DOMO.log('full_name: ' + obj.hris_id);
          datagrid.addCell(obj.full_name);
          //DOMO.log('first_name: ' + obj.first_name);
          datagrid.addCell(obj.first_name);
          //DOMO.log('last_name: ' + obj.last_name)
          datagrid.addCell(obj.last_name);
          //DOMO.log('value: ' + obj.value);
          datagrid.addCell(obj.value);
          datagrid.endRow();
        }
      }
      //if (obj2.links.custom_field.id == 5){
      //DOMO.log('Position is ' + obj2.value);

      //Moved this code inside the if id==4.

    }
  }

Upvotes: 1

Related Questions