How to save a changing response structure from external API in database?
![aleksis](https://us.v-cdn.net/6037124/uploads/userpics/RER3LTV2CB05/nFLCNPYAL2XPL.png)
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
-
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
-
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 receivecreditorAccount
as part of your data, Xano will save the record leaving this field empty, but respecting its structure. -
@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:
-
@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. -
Switching from dot notation to GET filter solved the problem.
If anyone else trying to do something similar, here's a great video from Michael
Categories
- All Categories
- 53 ? Announcements
- 47 ? Releases
- 37 ? Welcome
- 983 ? Help! I'm a Noob
- 125 ? No-Code Front-Ends
- 633 ? Working with APIs
- 439 ? Transforming data
- 126 ? Connect Xano to ...
- 50 ?? Find an Expert
- 348 ❓Other questions
- 35 ? Security
- 22 ✂️ Snippets
- 19 ? Showcase
- 7 ?️ Xano Chatter
- 62 ? Video Tutorials
- 171 ? Request a feature
- 229 ? Report a Bug
- 19 ? Templates & Extensions
- 7 ? Feedback