How to save a changing response structure from external API in database?

Options
aleksis
aleksis Member
edited September 2023 in ? Working with APIs

Hello!

I am trying to save a list of transactions from my bank account in my DB.

For example: here you can see that there is one incoming (account top up) and one outgoing transaction (paying a utility bill). One of them has a creditor name (utility provider) and another one has a debtor name (my own account from which I topped up).

This is how my function stack looks like. I am pulling the data from external API (which works fine and I can display results on screen), however, when I try to save it to DB, I run into a problem as in my loop I am expecting to have both, creditor account and debtor account saved as items.

How can I adjust my looping to allow to sometimes skip creditor account or debtor account if it is missing from the response structure and in other cases when it is provided save it in the database?

Best Answer

  • Ray Deck
    Ray Deck Trusted Xano Expert ✭✭✭
    Answer ✓
    Options

    I think the immediate problem you are having is because of the dot syntax vs the get filter. If you use item.creditorAccount, it will blow up if item does not have a member called creditorAccount. To "get if it exists",put item into the field, and then apply the filter "get" with a path of "creditorAccount". If the field is nullable, you can get away with leaving the default value as null.

    These kinds of API-heavy applications often deal with mutable data, so you don't know quite what you're going to get back. This makes tools like get and other conditional data manipulation filters (e.g. set_ifnotempty, first_ifnotempty) handy. Data manipulation is a strong suit for Xano, but it's still a complicated topic that's often part of our focus on the hardest 5% in our recorded office hours and forums on State Change

Answers

  • arturosanz
    arturosanz Member ✭✭
    Options

    You don't have to care about that if you defined correctly the table field types and input types. Whether an input is received or not, is not a problem for Xano. When it is not received Xano will store empty values according to the types defined.

    Just make sure the types match. You need to know the structure of creditorAccount in case it is received. For instance, creditorAccount → {"iban" : text} is an object with a text property called "iban". creditorAccount must be defined like so in the table you want to store it and in the inputs of the API endpoint.Then, if later on you don't receive creditorAccount as part of your data, Xano will save the record leaving this field empty, but respecting its structure.

  • aleksis
    aleksis Member
    edited September 2023
    Options

    @arturosanz appreciate your help, unfortunately I am still having the same problem.


    I updated the DB and now creditorAccount is an object
    *I left transaction amount as text, because if I chose integer, then it does not have decimals/cents for the transaction amount.

    the creditorAccount has a text type IBAN field.


    For the cases where the payload has creditorAccount and has it blank or populated - all is fine. Field is either saved or left blank.

    However, still when this object is not included in the payload I receive from external API (see below a transaction that does not have creditorAccount line at all) I get error and it breaks:
    message: Unable to locate var: item.creditorAccount

    I basically get all the transactions saved in DB up until -9.20 EUR (on the right side below) which is the first one to not have "creditorAccount" line at all.

    The function stack:



  • arturosanz
    arturosanz Member ✭✭
    edited September 2023
    Options

    @Ray Deck is right @aleksis , this error var: item.creditorAccount happens because there the dot notation requieres the existence of the path. Using filters instead won't throw an error.

    You have also the option to use the Try/Catch function to prevent errors to stop your functions' execution.

    By the way, you can use the decimal field type instead to not lose the decimals. It would be a good idea if you perform aggregations or calculations with those numbers.