Iterate properties of JSON reply

Iterate properties of JSON reply

  

Hey there,

I have searched online (albeit rather quickly I must confess) for a solution to this problem but I haven't found anything.


I'm using OpenExchangeRates (OER) to retrieve exchange rates in near real-time. However, the response from OER isn't an array with an entry per currency exchange rate. Instead it is a single object mapping the currency ISO code to the exchange rate. This means that OutSystems creates a single structure with dozens of attributes where each corresponds to a different currency.

Through a timer I am storing each exchange rate in the database for later usage. So this means that each time the timer fires I run the request for the exchange rates and then I have to iterate all currencies in order to turn them into an internal representation for my application. Currently I'm only interested in two of them, so I can manually extract and populate the data through a server action, however I want this to be extensible (for example: I add a new record to a Currency static entity - that maps to the ISO code of the currency - and the server action automatically starts taking that currency into account). If I was dealing with the JSON response through JavaScript I could just iterate the properties of the returned object / keys of the map. However I don't know how to proceed in OutSystems.


Thank you for your time,

Rui Rosário


TL;DR: OpenExchangeRates's API returns all exchange rates in a single JS map which gets converted to a single OutSystem structure with dozens of attributes (one per currency). I need to be able to dynamically iterate those attributes in the returned structure to process each value individually.


Note: I cannot just store the entire exchange rate structure in the database since I need to be able to query for the appropriate exchange rates on a per-currency basis.

Hi,


You can use the ardoJSON component to transform your json attributes into key value pairs. Check the JSON_Listify action of that component. Assuming you are using the a rest api, you can do that transformation in the OnBeforeRequest and transform the request body into a iterable format (and changing the action structure accordingly).


So in this example I called the JSON_Listify with the value from the input and passing "." in the Path parameter:

Regards,
João Rosado

Thank you,


After fiddling a bit with JSON_Listify I finally understand how it works and how to adapt my integration with OER for my needs. Handy module to keep around, ardoJSON.


Cheers,
Rui Rosário