Hey Guys,
it seems to be a beginner problem but I read every post here I could find and can’t figure it out.
What Iam trying to do:
Get some json with the webAPI [works]
Store the json in a variable / object
Use the data
Getting the json with the api works. But the app crashes when I try to save it (see red circle). I think the json I get is an array(?) because of the [ brackets]. So maybe iam storing it wrong with “getObjectFromJson”?
See below my components and the json (as returned by postman).
While debugging, move the blocks from the open event to a button click. Thunkable has issues with timing/repeated calls to the open event.
Does the alert dialog appear before crashing? If so ,what does it show? If not, try removing the alert message block and see what happens.
Try using 2 labels to see what is in response. Set one label to the response. Set another label to generate json from object response. What appears when you do this?
As I understand you are actually getting the response and you show it in the alert message but the app crashes when you attempt to use [generate JSON from object] block which makes sense because the output of your API is an array or a list (in Thunkable terms) and you need to assign it to a list, inside the list is an object which you can reference it using blocks from object drawer.
is the property and values are a part of url? if no first of all you are adding properties and value in url itself in web api there is a option of adding it
I have run into similar issues when working with Web APIs. The structure of the JSON is different on success vs. failure or depending on the parameters.
Identifying the variability in the responses is tedious work. Here are some blocks illustrating my approach:
First, check the error block. The error block is NOT a web service error, but a Thunkable internal error. I don’t think I’ve ever had a error that wasn’t null
Next, check the status. HTTP standards indicate series 200 codes are generally successes, while 400s are errors. For illustrative purposes, I’m assuming anything but 200 is an error. Your web sevice may be different.
Next, I check to see if the response is actually JSON by converting it to text and checking for {. This works even if the response is plain text. But it prevents errors that can occur if the response is NOT JSON.
Now that you have verified that the response at least MIGHT be JSON, now convert it to an object and check to see what properties are available. Sometimes the properties will change depending on the response. Some web serivces will even return a status 200 AND a response object that indicates there was a problem with the parameters. In this illustration, I first check to make sure the expected property exists. This will prevent errors associated with missing properties.
Again, this is not an exhaustive debugging routine, but it should give you an idea of how to validate the response BEFORE processing.
So iam pretty sure iam just not able to access the response I get from the webApi in the right way. When I execute the webapi get in postman or the browser my response looks like this:
The postman display shows that your response start with an array hence the “[” in the beginning. So you need to start with a list in Thunkable and do whatever you are doing to access the details
but it seems to work when I store the response in a variable with [get object from JSON]… So maybe you got me on the right track! I’ll try it not and post the results here.
So with the help of @drted and @muneer (thank you both and everyone who gave tips) the problem got solved. Just in case some comes here with a similar problem here is the solution:
Problem: the webApi responds with an array and the json/object is inside of this.
Solution:
Save the webApi response in a variable (this will be a list!)
Create a new object with properties from that variable