How to change the property of a nested object



I am trying to change the value of the property CZT in a nested object. I can’t figure out how to specify the path to this nested property. Can anyone help? Thank you.

I can help you but please post the full JSON response as text. That way, I can format it in https://codebeautify.org/jsonviewer to see the data structure more clearly.

Use the preformatted text button in the forum post toolbar to properly format the text (otherwise, smart quotes can make the JSON appear invalid):

image

Tatiang:
Here is a link to the project. The question pertains to the Cars Screen.

https://x.thunkable.com/copy/d90a49fe812c78ac6440c57a7acd6b1c`

Here (I hope) is the correct preformated text:

{"1":{"CZT":"00:00","LegTimeMinutes":0,"critique":"None","headingDegrees":999,"openTime":"00:00","typeWP":"Not yet specified","useCZT":false,"useHeading":false,"useOpenTime":false,"wpLatitude":40.123456,"wpLongitude":-89.123456,"wpPenalty":100},"2":{"CZT":"00:00","LegTimeMinutes":0,"critique":"None","headingDegrees":999,"openTime":"00:00","typeWP":"Not yet specified","useCZT":false,"useHeading":false,"useOpenTime":false,"wpLatitude":40.223456,"wpLongitude":-89.223456,"wpPenalty":100},"3":{"CZT":"00:00","LegTimeMinutes":0,"critique":"None","headingDegrees":999,"openTime":"00:00","typeWP":"Not yet specified","useCZT":false,"useHeading":false,"useOpenTime":false,"wpLatitude":40.323456,"wpLongitude":-89.323456,"wpPenalty":100},"4":{"CZT":"00:00","LegTimeMinutes":0,"critique":"None","headingDegrees":999,"openTime":"00:00","typeWP":"Not yet specified","useCZT":false,"useHeading":false,"useOpenTime":false,"wpLatitude":40.423456,"wpLongitude":-89.423456,"wpPenalty":100},"adminPassword":"","cztOffset":0,"dateOfEvent":"11/17/2022","maxScore":300,"registrationPassword":"abc","timingUnit":"Hundredths","CZT":"10:00"}

Tatiang,
I realized that I had not captured the entire object. Here is the entire text.

{"1":{"CZT":"00:00","LegTimeMinutes":0,"critique":"None","headingDegrees":999,"openTime":"00:00","typeWP":"Not yet specified","useCZT":false,"useHeading":false,"useOpenTime":false,"wpLatitude":40.123456,"wpLongitude":-89.123456,"wpPenalty":100},"2":{"CZT":"00:00","LegTimeMinutes":0,"critique":"None","headingDegrees":999,"openTime":"00:00","typeWP":"Not yet specified","useCZT":false,"useHeading":false,"useOpenTime":false,"wpLatitude":40.223456,"wpLongitude":-89.223456,"wpPenalty":100},"3":{"CZT":"00:00","LegTimeMinutes":0,"critique":"None","headingDegrees":999,"openTime":"00:00","typeWP":"Not yet specified","useCZT":false,"useHeading":false,"useOpenTime":false,"wpLatitude":40.323456,"wpLongitude":-89.323456,"wpPenalty":100},"4":{"CZT":"00:00","LegTimeMinutes":0,"critique":"None","headingDegrees":999,"openTime":"00:00","typeWP":"Not yet specified","useCZT":false,"useHeading":false,"useOpenTime":false,"wpLatitude":40.423456,"wpLongitude":-89.423456,"wpPenalty":100},"adminPassword":"","cztOffset":0,"dateOfEvent":"11/17/2022","maxScore":300,"registrationPassword":"abc","timingUnit":"Hundredths","CZT":"10:00"}
1 Like

Tatiang,
You will see several CZT’s in this object. I’m asking how to change the value of the first CZT in the object.

It was not formatted as pre-formatted text so it failed when I tried to format it but I just edited your post to format is properly and now you can paste that same text into https://codebeautify.org/jsonviewer to see the data structure:

image

When I click on the CZT key, it shows the path to that value at the top (object → 1 → CZT). So in Thunkable, you would set the property “[1].CZT” or set the property “CZT” of the first list item in the object.

I haven’t set JSON properties before so if it were me, I’d make sure I can get that key value (“CZT”) before attempting to set it to something else.

You can find out more by watching my tutorial video here: API JSON Tutorial (Video).

Do the brackets around the numeral 1 assume that 1, 2, 3, etc are a List? If they are not a list (they are just properties of an object), how would you specify the path to that first CZT? I’ve tried 1.CZT and that didn’t work.

Sorry, you’re right, that’s not a list/array. So you’d need to get the property “1.CZT” or you can use two Set/Get property blocks to first get the “1” property of the object and then get the “CZT” property from that.

This works for me:

To display this in a label:

image

This did not work:

It still displays 00:00.

But as I said, I don’t have any experience setting properties of objects. I assume that would have to happen at the server level and not internally within Thunkable variables.

1 Like

Tatiang,
I am able to get/read values from objects. What I haven’t been able to do is to change/store the value of a property in an object. I thought that “SetProperty” block was the proper choice but when I try SetProperty of property 1.CZT of the object, it appends a new property at the end of the object rather than changing the existing property.
Any suggestions for documentation or tutorial that would illustrate how to change the value of a property of an object?

Where are you getting the JSON response from? Are you using an API? Are you hosting the data yourself? Is this from Firebase? Is this from a physical bluetooth device?

I didn’t realize you’d included the project link… that’s helpful. But I can’t open it. Is it private?

I am not using an API. I create an object with several nested levels, including a list. This object is then stored on Firebase. This part is working. I then download the Firebase data back to an app object. I am able to read the values in this object and display them on the UI. My difficulty was occurring when I tried to change the value of a property (e.g. change CZT from 00:00 to 10:00).
Based on your help, I have now been able to accomplish this. Thank you so much for your help. I continue to be impressed with Thunkable.

No, it’s a public project.

Here is another link to this project. I checked the box agreeing to including passwords and API keys (I don’t believe the project has either of those items).

https://x.thunkable.com/copy/9ab670f895e9158a49dd6f5d2ed54c2e

1 Like

That one worked.

Since this is Firebase, you should be able to change a nested key’s value by using set cloud variable [NewEventName's Text]/1/CZT.

You’ll need to use a Join block to join NewEventName's text with the text string “/1/CZT”.

1 Like

@tatiang
What you did is correct. See my example

image

You cannot use get object from JSON to see the update as it will recompile the actual JSON not the updated one.

To go around it you need to
image
and work on the object directly.

Happy Thunking!

1 Like