dimanche 26 janvier 2020

How do I access children data in JSON response using Javascript/Gas

I am trying to access children data from a Json response to use it in an if statement but i don't know-how. Anyone know how to do this?

Here is the screenshot of the response and I have circled the object I want to access. I want only the summation to happen if the approval has a value i.e. status needs to be pending or approved, otherwise, no calculation will happen.

enter image description here

Here is the code that I use but don't know how to access the approaval={data=[{status}] form the JSON so as to use it

function showTimeData() {

    var users = getUsers()
    var endpoint = 'users/';
    var time_array = [];

    for (var i = 0; i < users.length; i++) {

        var url = 'https://api.10000ft.com/api/v1/users/' + users[i].id + '/time_entries?fields=approvals' + '&from=' + from + '&to=' + to + '&auth=' + TKF_AUTH;
        var response = UrlFetchApp.fetch(url, options);
        var info = JSON.parse(response.getContentText());
        var content = info.data;
        var total_hours = 0;


        for (var j = 0; j < content.length; j++) {
            if (content.data.approvals.data.length > 0) {
                hoursTotal = 0;
            }
            total_hours += parseInt(content[j].hours);
        }


        Logger.log('User name: ' + users[i].display_name + ' ' + 'User id: ' + users[i].id + '  ' + 'total hours: ' + total_hours)
    }

Aucun commentaire:

Enregistrer un commentaire