Remove Duplicated Rows in Rest API


#1

Hello,

Is it possible to remove duplicated rows of a field like city names,etc. when making request to rest API?

Like: SQL SELECT DISTINCT.

Thanks,


#2

By remove, do you mean delete them from the DB or eliminate duplicates from the API response?


#3

Hello @jeffreystables,
I want to just eliminate duplicates.


#4

Which database are you using? Can you provide an example of the call and the returned duplicate data as it is now?


#5

The database is MySQL.

When i call my table, it is like:

“id”:1 “City”: London
"id":2 “City”: Chicago
"id":3 “City”: Istanbul
"id":4 “City”: London
"id":5 “City”: Paris
"id":6 “City”: Berlin
"id":7 “City”: Istanbul
"id":8 “City”: London

as you see in “City” field, there are 3 London, 2 Istanbul, and other are unique.

When i call the api, I just want 1 London, 1 Istanbul and the rest of . Because i want to map them to a selectable menu.

Thanks


#6

Hello @jeffreystables,

Could you find a solution for my problem?

Thanks,


#7

It looks like “DISTINCT” isnt supported yet by DreamFactory.
Refer: Selecting Distinct Values

But we found a way on getting the unique values of an API response.

        window.df.apis.database.getRecords({
            table_name: "table"
        }, function (response) {
            var cityList = [];
            for (var index = 0; index < response.record.length; index++) {
                cityList = pushUniqueRecord(response.record[index], cityList);
            }

        }, function (response) {
                PGproxy.notification.alert(getErrorString(response), 'error', 'Error', 'OK');
        });



function pushUniqueRecord(data, cityList) {
    var found = false;
    for (var i = 0; i < cityList.length; i++) {
        if (cityList[i].City == data.City) {
            found = true;
            break
        }
    }
    if (!found) {
        cityList.push(data);
    }
    return cityList;
}

The pushUniqueRecord function will check if the cityList array has an object that has the same City element as to your response. If City is not found then that record will be push to the cityList array. If the response has same City element on the other record it will not push then. Then just use cityList.