Parsing JSON to Object problem

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).

1 Like

A few questions and suggestions.

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?

1 Like

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.

1 Like

This topic illustrates how to parse a JSON array:

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

yes, there are properties in the url. Does it make any difference whether I put them in the web api field or the url?

maybe try it in web api component itself

hey thanks for your reply,

so:

  • did add a button. changed nothing
  • yes, I can display the json response in a dialog or label. It crashes or replyes with “undefined” when I try to store or work with the response.

I can’t find a way to properly store or transform the list/json response.

What I tried now:

Sometimes the app crashes, sometimes I get an “undefined” or an empty object :sob:

yes you got that right :slight_smile: Thanks for your input. Still could not manage to solve it. See above

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.:frowning: 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.

Happy Thunking!

2 Likes

Hey Drted,
thanks alot for your response. I appreciate your efforts very much.

I rebuild you response check and learned a lot from it. I got the reply:

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:

I don’t know how to store these values in the right way! I would like to store it as an object with properties. But all I tried crashed the app.

Ideas how to do this the right way anyone?

1 Like

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

1 Like

Yes, as I wrote above I understood that it is a list. But I can’t access it :frowning: crashes everytime I try something like:

Any Idea how I get the values from the response (liste with 1 item) stored as an object with properties?

1 Like

You need to move [get object from JSON] inside the list

1 Like

thats not possible

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.

2 Likes

Also try it without [get object from JSON]. Sometimes the list component understand the structure.

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:

  1. Save the webApi response in a variable (this will be a list!)
  2. Create a new object with properties from that variable

3 Likes

Nice work @tmurachpx!

1 Like

Glad to see it solved

1 Like