53
Views
13
Comments
Solved
Consuming REST API and adding the result to the OS database
Question

I'm receiving data from currency rates API: https://api.exchangerate-api.com/v4/latest/USD

I can show it directly on the screen, but would also like to store it in the OutSystems database. When I am trying to add it there, the biggest chalenge is always different data types (same with adding them to a Record List). There are 2 types of data for each currency (record): Currency code (text), and Rate itself (Decimal). I created an entity with the same structre. Then I created Data Action with an Output Parameter with a corresponding data type (Rate). When I am trying to transfer the data from API it says the data type should be Rate List instead of Rates, but I can't change it. I also tried adding JSONSerialize to the action chain, but it just converts the data into string.  


090325223154.jpg
2022-12-30 07-28-09
Navneet Garg
Solution

You can use ardoJSON in OnAfterResponse of API to change json format.

https://jnb.outsystemscloud.com/ardoJSONReactiveDemo/JSON_Listify


and than create structure 

If you can share oml I can help you on this.

2025-10-06 19-50-31
Muslim Sattarov

Thanks @Navneet Garg, earlier I tried JSONListify of ardoJSON. I put it after JSONSerialize and then received a JS array of objects in a string, like this ([{},{},{}]). So, I used JavaScript JSON.parse() to convert it to an actual array and show in on the screen. Oh, and I put the action OnRender, which I don't think was right, is it better be placed OnAfterResponse from API?

I shared oml in a previous post. Can you take a look please? 

2019-01-07 16-04-16
Siya
 
MVP
Solution

@Smus, based on @Navneet Garg suggestion, here is the implementation in your code. 

CurrencyRates.oml
2019-01-07 16-04-16
Siya
 
MVP
Solution

@Smus, based on @Navneet Garg suggestion, here is the implementation in your code. 

CurrencyRates.oml
2022-12-22 10-00-39
Beatriz Sabino

Hi, Smus.

Is it possible to share the .oml file to better understand what might be the problem?

2024-09-09 11-16-05
GVenkat

Hi @Smus ,

If you only need the exchange rates, then you can use the below solution.

2025-10-06 19-50-31
Muslim Sattarov

Yes, that was my first thought (I used CreateOrUpdateCurrencyRates though).

I have 2 values: currency and rate there, so I can't do the same, because I need a List-like variable.

Shared the oml. 

There's another logic to show rates directly without processing the API data. Don't pay attention to it. The action I created trying to store it in a database is called 'GetRatesFromAPI'. The entity created with 2 attributes, is 'CurrencyRate'.

CurrencyRates.oml
2022-12-30 07-28-09
Navneet Garg
Solution

You can use ardoJSON in OnAfterResponse of API to change json format.

https://jnb.outsystemscloud.com/ardoJSONReactiveDemo/JSON_Listify


and than create structure 

If you can share oml I can help you on this.

2025-10-06 19-50-31
Muslim Sattarov

Thanks @Navneet Garg, earlier I tried JSONListify of ardoJSON. I put it after JSONSerialize and then received a JS array of objects in a string, like this ([{},{},{}]). So, I used JavaScript JSON.parse() to convert it to an actual array and show in on the screen. Oh, and I put the action OnRender, which I don't think was right, is it better be placed OnAfterResponse from API?

I shared oml in a previous post. Can you take a look please? 

2019-01-07 16-04-16
Siya
 
MVP
Solution

@Smus, based on @Navneet Garg suggestion, here is the implementation in your code. 

CurrencyRates.oml
UserImage.jpg
Gireesh Kumar  P

You can achieve this by using JavaScript for each loop.  I have tried it with the following code.

Get the rates structure value in data action output and pass it to JavaScript after serializing. And the output must be deserialized using a list of structure with attributes CurrencyCode and Value.





2019-01-07 16-04-16
Siya
 
MVP

@Gireesh Kumar  P : Even though your solution works this involves an additional round trip from client to server as the JavaScript works on the client side. Also looping on the client side to insert data to DB is not a good practise.  @Navneet Garg has suggested a better solutions and it should work using JSON_Listify.

UserImage.jpg
Gireesh Kumar  P

Hi @Siya , Just asking for clarification. What if I wrap looping and create action inside a server action like below.



2019-01-07 16-04-16
Siya
 
MVP

Hi @Gireesh Kumar  P, here the APIs are executed on the server side and hence the data is also on the server side. To convert the data to list of Key Value pair it is an over kill to bring the data to client side. Also it will not suit in a scenario where this job is running in a process or timer. 

Regarding inserting in a loop, I would suggest a bulk insert using the forge component BulkInsert.

2020-07-29 19-08-40
Sanjay Kumar Sahu

Hello Smus,

In my opinion, The easiest way is to create a Data action with Rate List type output and assign your Rest API response to this. Response is clearly a List with Key value pair of Text and Decimal so defining only Rate type not gonna work. If you're not able to change type than delete and create a new Data action with Rate List type output. 

Coming to saving data in DB, once you will have List, you can either loop through each element and call Create Entity action or you can use BulkInsert component, perhaps you barely have 100 items, looping won't cause performance issue. I believe your entity have attributes of type text and decimal, so while assigning data, you shall use data conversion for safe side.

2024-12-27 06-34-00
Suryanarayana Vundru

Hi Smus ,

You can use Java Script to achieve this.

Instead of using Id as Primary key use currency as primary key with text data type.

check below oml file for solution. 


Cheers,

Surya

CurrencyRates.oml
2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Hi Suryanarayan,

This topic is already a few days old, did you bother to read all the replies? Because a good solution has already been given, and Siya explained here why the solution you propose (and was already proposed), is a bad idea!

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.