Changing the returned JSON format


#1

I get this:

{“record”:[
{“sID”:125,“cName”:“Cornell”,“major”:“EE”,“decision”:“Y”},
{“sID”:345,“cName”:“Cornell”,“major”:“CS”,“decision”:“Y”}
],
}

I want this

result = {[
{“sID”:123,“cName”:“Cornell”,“major”:“EE”,“decision”:“Y”},
{“sID”:345,“cName”:“Cornell”,“major”:“CS”,“decision”:“Y”}
],
}

Where do I look in dreamfactory code to mofiy the returned json.
Thanks David.


#2

The returned dataset is wrapped so that meta data such as counts and schema can also be returned if requested (also allowing for additional parameters to be added when posting datasets back to the server). So be aware that if you remove the wrapping, some API requests will no longer work as designed.

With that being said, wrapping is defined and used in the BaseDbSvc.php class, look for “RECORD_WRAPPER”, which can be found in the lib-php-common-platform library /src/Services/BaseDbSvc.php. This library is included in the build package under vendor/dreamfactory.

Another option would be to write a very small Server Side Script to strip off the wrapper in all or certain scenarios. These scripts are javascript, so you don’t have to modify the PHP.


#3

I have the same query (more or less).

I want to take this response from getRecordByFilter() –

{"record":
   [{"id":1,"first_name":"Beau","surname":"Scott","birthdate":"","address":"7127 Sociosqu Ave","postcode":"FN4Y 9GH","nhs_number":2147483647,"gender":"Male"},        {"id":2,"first_name":"Garrison","surname":"Sheppard","birthdate":"","address":"906-7498 Facilisis St.","postcode":"BD4J 6OX","nhs_number":1611531426,"gender":"Male"},
  {"id":3,"first_name":"Erich","surname":"Crane","birthdate":"","address":"231-2271 Curabitur St.","postcode":"U9G 8WA","nhs_number":2147483647,"gender":"Male"}]
}

And transform/parse it to:

 [{"id":1,"first_name":"Beau","surname":"Scott","birthdate":"","address":"7127 Sociosqu Ave","postcode":"FN4Y 9GH","nhs_number":2147483647,"gender":"Male"},        {"id":2,"first_name":"Garrison","surname":"Sheppard","birthdate":"","address":"906-7498 Facilisis St.","postcode":"BD4J 6OX","nhs_number":1611531426,"gender":"Male"},
  {"id":3,"first_name":"Erich","surname":"Crane","birthdate":"","address":"231-2271 Curabitur St.","postcode":"U9G 8WA","nhs_number":2147483647,"gender":"Male"}]

except I would prefer to do it client-side, ideally by taking the response and just stripping out the meta-data.

I am very new to coding in general let alone javascript. However, I want to learn. Consequently, would you @leehicks (or anyone else) point me in the right direction on how you would go about doing this?

I suspect there are others who would be interested in this too. I would be happy adding what I learn to the documentation.

Many thanks in advance.

@gdvallance


#4

Which sdk are you using? Javascript? Angular?


#5

Thanks. I am using javascript/jQuery.

Interested in trying Angular …

@gdvallance


#6

I have managed to work it out for myself :smile:

It turns out to be very simple. Apologies in advance if I am not using the correct terminology …

DreamFactory (DF) returns an object in JSON format from a REST call. You can strip out the {“record”: … } through using the dot notation for objects. For instance if you assigned response to a variable you can strip out the record by response.record, and choose a particular record through its array index. For instance response.record[0] would pull out the first record and response.record[1] would pull out the second, and so forth.

E.g. response.record[0] would pull out:

{"id":1,"first_name":"Beau","surname":"Scott","birthdate":"","address":"7127 Sociosqu Ave","postcode":"FN4Y 9GH","nhs_number":2147483647,"gender":"Male"}

So if you wanted to take a response from getRecordsByFilter() and turn it into an array to pass into jQuery library for presentation on a page you could do something like this:

window.app.getPatients = function () {
    window.df.apis.db.getRecordsByFilter({table_name: "MockPatients",limit: "20"}, function (response){
        var report = [];
        for (var i=0; i<response.record.length; i++) {
            report.push(response.record[i]);
        }
        $('#patients-table').columns({
            data: report,
            schema: [
                {"header":"ID","key":"id"},
                {"header":"First Name","key":"first_name"},
                {"header":"Surname","key":"surname"},
                {"header":"DOB","key":"birthdate"},
                {"header":"PostCode","key":"postcode"},
                {"header":"NHS No:","key":"nhs_number"},
                {"header":"Gender","key":"gender"}
            ]
        });

What I am going above is passing the response from DF into a jQuery-based project called Columns that will nicely turn the data into a table. See: https://github.com/eisenbraun/columns

Columns will only take an array. It won’t take an object which is why I asked the question …

Hope this is useful to someone else.

@gdvallance


#7

Yes. This is the simplest way to do it. :smile:


#8

Is there is anything which is good about Dream Factory???The folders you are telling these are not exist.I am using dream factory 2.4.2