How can we help you today?

Map Results Function for Your Custom ShortPoint REST APIs

If you tried to use the REST API connection type before, you may have noticed that it works with some connections while it does not work with others, that's because every REST API service provide the data in its own way and sometimes ShortPoint cannot easily understand what is the data coming out of your REST API service. So if you were trying to connect to a REST API and do not see the data coming out in ShortPoint, then this article is for you.


Issue

Connecting to some REST APIs sometimes might not work, because ShortPoint expect the API to return data encapsulated inside an array, while the REST API might return data in another formats.


So for example, connecting to a REST API that provides the following data, simply will not work as expected:


{
  "result_data": [
    {
      "id": 1,
      "weather": "Clear",
      "date": "2018-09-03",
      "minTemp": 31.6,
      "maxTemp": 41.315,
      "temp": 42.1,
      "humidity": 35
    }, {
      "id": 2,
      "weather": "Clear",
      "date": "2018-09-04",
      "minTemp": 32,
      "maxTemp": 40.5,
      "temp": 41.1,
      "humidity": 37
    }
  ]
}


Because the returned data is encapsulated inside an object {}, and not inside an array [], although this object does contain an array that has the right values for the connection result_data, but it's not easy for ShortPoint to figure this out, and if you switch to the ShortPoint fields, you will notice that ShortPoint does not quite understand the data structure behind your REST API services.



Solution


To fix the data that is returned from our sample REST API service in a way that ShortPoint can understand, we need to process the data in the previous example to become as follows:

 

[
  {
    "id": 1,
    "weather": "Clear",
    "date": "2018-09-03",
    "minTemp": 31.6,
    "maxTemp": 41.315,
    "temp": 42.1,
    "humidity": 35
  }, {
    "id": 2,
    "weather": "Clear",
    "date": "2018-09-04",
    "minTemp": 32,
    "maxTemp": 40.5,
    "temp": 41.1,
    "humidity": 37
  }
]



And we can do this simply by using the Map Results function in the Connect Tab, 



So what does this Map Result function do?

After successfully connecting to the REST API service, and right before passing the resulted data into the ShortPoint element, this little function will be executed, allowing you to process the resulted data, so you can pass the right data to the ShortPoint element.


This function has only one argument (data), which is the data coming from the REST API Service.


If you don't provide anything in the Map Result function, by default it will have the following value


return data;


Which means return the exact data that we had from the REST API service, without any modification.


Taking the previous example into account, we wanted to pass the inner result_data array to the ShortPoint element, instead of the root object, 

So we can simply do that by writing the following snippet inside the Map Results function


return data.result_data;



Once we do that, and click on the connect, we should see the right processed data displayed in the Data Example area in the connect tab, sounds good!



And if we switch back to the ShortPoint fields, we will see that it is reading the correct value of the columns as well.



Hope that helps,

Happy connecting!



Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.

World's best intranet sites are designed using ShortPoint

Get started today! Learn more
See all 31 topics

Start a trial

Ignite your vision. Install ShortPoint directly on your site, or play in sandbox mode. No credit card required.

Get started today

World’s best intranet sites are designed using ShortPoint

Thousands of companies using ShortPoint everyday to design, brand and build award winning intranet sites.

Get started Learn more