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 connec to a REST API and do not see the data coming out in ShortPoint, then this article is for you.


The Problem:

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 its not easy for ShortPoint to figure this out, and if you switch to the ShortPoint fields, you will that ShortPoint does not quite understand the data structure behind your REST API servicec



The 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 Resulst function in the Connect Tab, 



So what does this Map Result function does?

After succcessfully 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 shoud 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!