Populate local datasource table dynamically

Is it possible to update a local data source table dynamically based on a web api response or by by downloading data from a google sheet ,

Yes,

Place the imported data into variables and then use the Create Row block under Datasources.

1 Like

ok , that’s a great solution .
but when I tried it didn’t work, and here is the blocks, I’ve tried 2 strategies but none of them works , So there is a missing thing here,
and here is the api url and it works fine
https://script.google.com/macros/s/AKfycbwhsM1QA6Z9sYuAewzzBtR9tIMWLeKGl_949OD804TCB8xFBXODQrlSAE1IQX3EijRh/exec



Any Idea how to solve it.

Hi,

I came up with this so far

https://x.thunkable.com/projects/650d9b530196d37537ea9c77/f0ee3329-ddd4-4e84-9c4c-92b4374e1dc1/blocks

which extracts a specific row from the JSON response. I just dropped the responses in labels for testing purposes.

You can have a look here for an explanation of parsing the data especially when it is in sub-trees.

Where I am stuck though, is trying to figure out how to return the number of entries under the [data] object so that you can loop through.

@tatiang responded to me in previous post about this, but I cannot get the correct set of blocks put together to return the 1001 result.

I am just a beginner so maybe someone else with more experience than myself can pitch in.

Steve

The problem is that the list you want is inside of the “data” property but you’re not referencing that with your blocks. You’re attempting to get the list from the object but you need to get the list from the “data” property of the object. You can see that in this screenshot from Best JSON Viewer and JSON Beautifier Online where I pasted in your JSON response text (the path to the “Full Name” property is shown at the top of the screenshot):

A fix for this would be to set app variable Response EEID to get property “data” of get object from JSON [response].

I have a JSON parsing tutorial here: API JSON Tutorial (Video)

Hi @tatiang,

Thanks for the response and I had watched the video before.

The blocks that I have to extract the data into the labels work fine; it is getting the count from the data list that is my problem.

But following the logic, if I get the properties “Full Name”, etc. from the data list, then shouldn’t I just be able to get the length of the list using this -

When I do this, I get a value of 1.

You’re on the right track but you made a mistake by adding a [gear] list block. The value of the get property block is already a list so when you add that [gear] list block in front of it, you create a list of lists. That’s going to cause problems. Just delete the [gear] list block and it should work.

It’s a good rule of thumb to never use the [gear] list block unless initializing or setting a list variable in order to define its contents.

I tried this one but still is not working

It was great to receive your help, and it would be better to make the blocks running inside the create new row block ، especially as it was working fine to extract the data from api in the above example,
I’m really appreciating your help

That’s because of the for loop. The variable Response is non-numeric so that loop cannot run. This is why I am trying to get the length of the list to set to a variable that you can then use to run the for loop.

@tatiang Beautiful! Thanks, that worked.

@minamaestro2023kg and @drmina2007er0sjg see blocks below. I tried the “for each” block but that didn’t work, so I get the size of the list and then use a “count with” block.

So to push these into a local data source, instead of showing the value in a label, set a variable to the value from the JSON and the “create row” from datasources to inject the values.

This was a good learning experience for me as well!!

Your solution steve works like magic in populating labels, for create row in data source, is not working .
So I think there is a problem in the create new row in data block.
here is screenshot


And here is the project itself so maybe you can tinker about it
https://x.thunkable.com/copy/3b09a6ac8278453d96f2cbccf1cabd13

@minamaestro2023kg Yep, when I tried it nothing was created in the Local DB, so I tried a Google Sheet and could see the data being updated in real-time.

Went back and tried both App Data Source and Stored Data Source for the Local DB and nothing - the count does not even increment on the label (actually the screen goes blank). Not too sure why this is - would be good to know. Will ping @tatiang and see if he can shed some light.

Anyway, I believe that a Google Sheet may be more useful as it can be accessed outside of the Thunkable ecosystem.

https://x.thunkable.com/projects/650d9b530196d37537ea9c77/570eef0c-6c45-4747-aa53-d40d443ff778/blocks

The create row block does not have a built-in delay except for the “do” section of the block. So if you put a create row block inside of a loop, it will attempt to create a new row every few milliseconds which will cause problems. The create row block needs time to complete its functionality. The easiest way to fix that is to add a wait block inside of the loop. You can experiment with delay values but sometimes 0 seconds is enough. Sometimes it needs to be 0.01 or even 0.03 seconds.

Putting the wait block inside of the “do” section of the block fails because the loop still continuously calls the create row block.

[Aside: You can see this effect without a loop, too. Just put a change label's text block after/below a create row block and in the “do” section put another change label's text block. Give them different values and see which one gets changed first. It works best to join a unique word with the seconds since 1970 block for each label. That way you actually get a timestamp for when the block triggered.]

1 Like

Thanks for response!

@tatiang that’s really Solved the problem, and here is the demo app


https://x.thunkable.com/copy/eb027fabe2ff5f82df9445af24897f2f
but I have a note why after refreshing the app, the data source loses automatically all the loaded data from the API response as you can try yourself.

1 Like

@tatiang @steve_synopsis, I really appreciate your help and dedication

1 Like

What do you mean by “refreshing the app”? Do you mean quitting and re-opening it? Or do you mean reloading the browser page? Data added to a local data source will remain in that data source until you delete it. But you won’t be able to see the new/modified data by clicking on the Data button in the Design tab of your project. That button only shows the initial state of your data source before any changes were made using blocks.

@tatiang exactly, when you check the local datasource icon after API response I can’t find any data except for the initial data,
Are there any way to check the local datasource content.

No, there is no way to view the current data in a local data source while editing a project. It’s been a feature request but has no been added yet.