Converting a record of multiple of the same structures into a list of that structure

Hello,

I am trying to get information from this web service https://www.alphavantage.co/query?function=TIME_SERIES_MONTHLY&symbol=MSFT&apikey=demo and as a response I receive 100 separate structures. What I want, if it is possible, is to convert the response into a list of that structure without having to append 100 times.

Attached, you can see a picture of the actual response.

Do you have any ideas on how I should proceed?

Thank you in advance for your answers.

Kind regards,

Larisa



Solution

Hi Larisa,

That's a f*cked up REST service if ever I saw one... There's unfortunately no out-of-the-box solution for this. I think the most sensible way would be to add an On After Response handler and convert the JSON to something OS can handle.

Solution

Hi Kilian,

While checking the documentation, I also found this solution before asking, but I was hoping for an easier one.

Thank you for your response.


Kind regards,

Larisa

Hi Larisa,

Sorry that I can't make it any easier :). But it really is an extremely weird format... Good luck!

Kilian Hekhuis wrote:

Hi Larisa,

That's a f*cked up REST service if ever I saw one... There's unfortunately no out-of-the-box solution for this. I think the most sensible way would be to add an On After Response handler and convert the JSON to something OS can handle.


Hello Kilian,


I'm struggling with the same problem but lack the skills on how to design the After Respons handler. Could you give some hints on how to do this?


Regards Bouke

Hi Bouke,

If you have a REST answer like this, the person that did it is lacking skills much more than you...

The hint we can provide is:

The OnAfterResponse has one input parameter that contains the JSON text, and one output parameter you have to set up with the final JSON text OutSystems will use to create the output of the method (in an Output format, List, etc).

So, you need to manipulate the text of the JSON (there are many text functions you can use for that), modifying it to a "reasonable" JSON.

For example, the JSON in this topic:

{
    "Meta Data": {
        "1. Information": "Monthly Prices (open, high, low, close) and Volumes",
        "2. Symbol": "MSFT",
        "3. Last Refreshed": "2019-08-09",
        "4. Time Zone": "US/Eastern"
    },
    "Monthly Time Series": {
        "2019-08-09": {
            "1. open": "137.0000",
            "2. high": "140.9400",
            "3. low": "130.7800",
            "4. close": "137.7100",
            "5. volume": "230901000"
        },
        "2019-07-31": {
            "1. open": "136.6300",
            "2. high": "141.6800",
            "3. low": "134.6700",
            "4. close": "136.2700",
            "5. volume": "484547500"
        },
        "2019-06-28": {
            "1. open": "123.8500",
            "2. high": "138.4000",
            "3. low": "119.0100",
            "4. close": "133.9600",
            "5. volume": "508324300"
        },
        ...

Should be changed to something like:

{
    "Meta Data": {
        "1. Information": "Monthly Prices (open, high, low, close) and Volumes",
        "2. Symbol": "MSFT",
        "3. Last Refreshed": "2019-08-09",
        "4. Time Zone": "US/Eastern"
    },
    "Monthly Time Series": [
         "data":{
            "date:": "2019-08-09",
            "1. open": "137.0000",
            "2. high": "140.9400",
            "3. low": "130.7800",
            "4. close": "137.7100",
            "5. volume": "230901000"
        },
        "data":{
            "date:": "2019-07-31",
            "1. open": "136.6300",
            "2. high": "141.6800",
            "3. low": "134.6700",
            "4. close": "136.2700",
            "5. volume": "484547500"
        },
        "data": {
            "date":"2019-06-28",
            "1. open": "123.8500",
            "2. high": "138.4000",
            "3. low": "119.0100",
            "4. close": "133.9600",
            "5. volume": "508324300"
        },
        ...
    ]
}

Now you have an array of a standard structure.

Hope this helps.

Cheers.

P.S. This is just an example, not necessarily the best structure output...