# Generating/ changing JSON object

**URL:** https://community.thunkable.com/t/generating-changing-json-object/106049
**Category:** Questions about Thunkable X
**Created:** [June 14, 2019, 10:28am UTC](https://community.thunkable.com/t/generating-changing-json-object/106049 "2019-06-14T10:28:42Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![John\_In\_WI](https://avatars.discourse-cdn.com/v4/letter/j/c37758/32.png) [@John\_In\_WI](https://community.thunkable.com/u/John_In_WI)
#### Post date: [June 14, 2019, 10:28am UTC](https://community.thunkable.com/t/generating-changing-json-object/106049/1 "2019-06-14T10:28:42Z")

</div>

The JSON I send via Web\_API needs to look something like this:  
{ “in”: { “rcmd”: “comm”, “param1”: “0”, “param2”: “0”, “param3”: “0”, “param4”: “0”, “param5”: “0” } }

The only way I’ve figured out to get an object to generate that (with the extra curly braces) is:

![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/c/1/c105016fe8d8f63b9ffe905e69eb9e842495f074.png)

How do I then set the properties later of the “inner” object? (rcmd, param1, para2, etc)

thanks!

---

<div class="post-metadata">

### Author: ![jane](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/jane/32/27575_2.png) [@jane](https://community.thunkable.com/u/jane)
#### Post date: [June 14, 2019, 11:33am UTC](https://community.thunkable.com/t/generating-changing-json-object/106049/2 "2019-06-14T11:33:31Z")

</div>

Hi there,

You’ve found a problem with Thunkable X 😅  
You can change the property of an object, so you can change ‘in’ to something else no problem.  
But right now you can’t change the property of a property of an object, so changing rcmd, param1, etc. isn’t possible with the ‘set property’ blocks at this time!

You’d have to use a kind of awkward solution where you change ‘in’ to be a whole new object every time, like this:  
 ![props22](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/5/4/543847c2dbcac5563694d602697c1a87a1b3acb0.png)

We know this is awkward, especially when your property has a lot of properties like ‘in’ does in your object, but it’s the best you can do right now!

We are working on this problem, so hopefully you won’t have to use a workaround for long!

Thanks,  
Jane

---

<div class="post-metadata">

### Author: ![John\_In\_WI](https://avatars.discourse-cdn.com/v4/letter/j/c37758/32.png) [@John\_In\_WI](https://community.thunkable.com/u/John_In_WI)
#### Post date: [June 14, 2019, 12:01pm UTC](https://community.thunkable.com/t/generating-changing-json-object/106049/3 "2019-06-14T12:01:00Z")

</div>

Thanks for the quick response!

What about a non-object solution?  
Maybe you can help in this instance.  
This works:  
 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/8/3/832c9ee2e82f405bec15376ec58167957829fa07.png)  
The webapi seen to recognize this is JSON content type.  
BUT when I do this:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/5/f/5f9c9e5ba8b05c79a531183fa560d10bc6beed18.png)  
It does not.  
When I look at the string formed, it is formatted correctly, but I believe the content type is not JSON in this case.  
How might I “set” the webapi to ‘Content-Type’: ‘application/json’ ?

---

<div class="post-metadata">

### Author: ![actech](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@actech](https://community.thunkable.com/u/actech)
#### Post date: [June 14, 2019, 12:45pm UTC](https://community.thunkable.com/t/generating-changing-json-object/106049/4 "2019-06-14T12:45:18Z")

</div>

I see an error in the last example - “”, “param5”:" Must be - “, “param5”:”

---

<div class="post-metadata">

### Author: ![John\_In\_WI](https://avatars.discourse-cdn.com/v4/letter/j/c37758/32.png) [@John\_In\_WI](https://community.thunkable.com/u/John_In_WI)
#### Post date: [June 14, 2019, 1:05pm UTC](https://community.thunkable.com/t/generating-changing-json-object/106049/5 "2019-06-14T13:05:40Z")

</div>

Wow! nice catch!  
That works.  
I _think_ I can use this solution for now.  
Thanks very much

---

<div class="post-metadata">

### Author: ![Mark](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/mark/32/18133_2.png) [@Mark](https://community.thunkable.com/u/Mark)
#### Post date: [June 15, 2019, 11:27pm UTC](https://community.thunkable.com/t/generating-changing-json-object/106049/6 "2019-06-15T23:27:14Z")

</div>

@John_In_WI, since you already have a function defined with all the appropriate parameters, why not just create the object that you want to send using those parameters and then convert it to JSON using the `'generate JSON from object'` block? I strongly suggest that approach, since then you don’t have to worry about generating the correct JSON syntax yourself! Plus, if you ever have to add any more parameters it will be much simpler to add it to the `'create object'` block than to the `'join'` block.

-Mark

---

<div class="post-metadata">

### Author: ![John\_In\_WI](https://avatars.discourse-cdn.com/v4/letter/j/c37758/32.png) [@John\_In\_WI](https://community.thunkable.com/u/John_In_WI)
#### Post date: [June 17, 2019, 8:07pm UTC](https://community.thunkable.com/t/generating-changing-json-object/106049/7 "2019-06-17T20:07:08Z")

</div>

Mark-  
I _think_ that’s what I tried to do at the top of this thread?  
The JSON data I need to POST is “nested” and I can’t change the arguments of the object of the nested part. That nested data is the stuff that needs to change with every button click.

Otherwise, you’re spot on, that’d be a much better way to go, for all the reasons you mention.

Then again, I’m new to this whole Thunkable thing and definitely don’t know the ropes yet and very likely am missing many things that are obvious.

Using it this week with a classroom full of kids that are triggering robot (and other IOT things) functions with Thunkable. So far so good.

---

<div class="post-metadata">

### Author: ![Mark](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/mark/32/18133_2.png) [@Mark](https://community.thunkable.com/u/Mark)
#### Post date: [June 17, 2019, 8:21pm UTC](https://community.thunkable.com/t/generating-changing-json-object/106049/8 "2019-06-17T20:21:36Z")

</div>

> [@John\_In\_WI](#):
>
> The JSON data I need to POST is “nested” and I can’t change the arguments of the object of the nested part. That nested data is the stuff that needs to change with every button click.

I was suggesting just recreating the entire object (in your `'sendToRobot'` function) rather than changing parts of it, and generating your JSON from that.

Hope this helps!

-Mark

---

<div class="post-metadata">

### Author: ![domhnallohanlon](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/domhnallohanlon/32/29554_2.png) [@domhnallohanlon](https://community.thunkable.com/u/domhnallohanlon)
#### Post date: [March 23, 2020, 10:25pm UTC](https://community.thunkable.com/t/generating-changing-json-object/106049/9 "2020-03-23T22:25:02Z")

</div>

3 posts were split to a new topic: [Why won’t my loop evaluate this logic?](https://community.thunkable.com/t/why-won-t-my-loop-evaluate-this-logic/531452)

---

<div class="post-metadata">

### Author: ![drted](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/drted/32/92694_2.png) [@drted](https://community.thunkable.com/u/drted)
#### Post date: [April 21, 2020, 5:46pm UTC](https://community.thunkable.com/t/generating-changing-json-object/106049/10 "2020-04-21T17:46:29Z")

</div>

Has the problem of SETting nested Objects been resolve yet?

---

<div class="post-metadata">

### Author: ![athornock](https://avatars.discourse-cdn.com/v4/letter/a/b4bc9f/32.png) [@athornock](https://community.thunkable.com/u/athornock)
#### Post date: [April 27, 2020, 4:58pm UTC](https://community.thunkable.com/t/generating-changing-json-object/106049/11 "2020-04-27T16:58:58Z")

</div>

I don’t believe so.

---

<div class="post-metadata">

### Author: ![drted](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/drted/32/92694_2.png) [@drted](https://community.thunkable.com/u/drted)
#### Post date: [May 5, 2020, 10:56am UTC](https://community.thunkable.com/t/generating-changing-json-object/106049/12 "2020-05-05T10:56:07Z")

</div>

![KeyPath2](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/b/b/bbc35c13aaac1ea22ed72f48ec489d661e63aa4f.png)

Working with complex nested object hierarchies performing multiple updates while using variables that have global application scope gets ugly quickly. Therefore I came up with 2 recursive functions which emulate the “Dictionary” blocks from MIT App Inventor 2 of “Get Property For Key Path” and “Set Property For Key Path”. This version does not have any error handling, so if the path is wrong, the update fails without notifying the programmer (but x.Thunkable blocks are the same). At least they get the job done with a LOT fewer blocks.

Each function requres a separate app level variable for the functions to work correctly. The click event and the DictionaryUsers list are there to illustrate the use and are not required. I’ve only done initial testing. If anyone comes up with improvements, please post for us all to enjoy.

An unfortunate limitation is that although variables are global, function are not. The way I use it is I create a form called Dictionary to hold the variables and keep copies of the functions. When another form need the function, it can be copied from the Dictionary form to the target form. Do NOT copy the variables, just the function you need.

I have also saved the example form as a public project at [Thunkable](https://x.thunkable.com/copy/19b2ea706648f806f2ccf5e5eecb1e01)

Happy blocking!

Ted

---

<div class="post-metadata">

### Author: ![jared](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/jared/32/138473_2.png) [@jared](https://community.thunkable.com/u/jared)
#### Post date: [May 5, 2020, 11:03am UTC](https://community.thunkable.com/t/generating-changing-json-object/106049/13 "2020-05-05T11:03:26Z")

</div>

Hey guys! Check this out. Does this work for what you’re wanting?

[https://x.thunkable.com/copy/5f708f3a2ef79c8c477a8d202b3ed03a](https://x.thunkable.com/copy/5f708f3a2ef79c8c477a8d202b3ed03a)

---

<div class="post-metadata">

### Author: ![drted](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/drted/32/92694_2.png) [@drted](https://community.thunkable.com/u/drted)
#### Post date: [May 6, 2020, 11:01am UTC](https://community.thunkable.com/t/generating-changing-json-object/106049/14 "2020-05-06T11:01:07Z")

</div>

Thanks for the link jared! The UI is very slick.

I see a few challenges for me to use your solution in my apps. It looks like your app creates a project and a sub project only (2 levels).How does your app handle deep hiearchies (3+ levels)? It also looks like you have hardcoded the number of properies. What if the JSON hiearchy is not static or under development? Finally, I’m haivng a hard time figuring out how I coould incorporate your solution into an existing project.Your solution appears heavily reliant on the UI and there are a LOT of blocks. I’m not sure how I would reuse this solution within mulitple screens in an existing app.

---

<div class="post-metadata">

### Author: ![ioannis](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/ioannis/32/146956_2.png) [@ioannis](https://community.thunkable.com/u/ioannis)
#### Post date: [November 8, 2024, 11:29am UTC](https://community.thunkable.com/t/generating-changing-json-object/106049/15 "2024-11-08T11:29:25Z")

</div>


