modifying JSON settings

183 views
Skip to first unread message

​ ​man

unread,
May 30, 2023, 5:15:21 PM5/30/23
to Automate
1) I have a variable settings (at flow beginning) with default values; it is a JSON dictionary = {"modules": {filter:0, check:0, reject:0}, regexp: "sth"} (and who knows what else will be there in the future).
2) Then I ask user (using Choice dialog block with multiple choices) which modules he wants to use.
3) Then I want to modify values in settings["modules"] according to user choice.

I've tried using For each block but I can't get it work. I used it with Variable set block and Dictionary put block but they don't let me input square brackets (settings["modules"]).
Am I doing sth wrong or I have to make a workaround? I suppose there are multiple ways (I found 2) that achieve this using many more blocks and/or variables, for example: 1) create (in the For loop) a temporary dictionary with modified values and put it later in the settings dictionary; or 2) not use the For loop, but use separate Expression true+Variable set for each variable independently.

​ ​man

unread,
May 30, 2023, 5:18:29 PM5/30/23
to Automate
There comes another, more general question.

How to modify JSON contents? I see that Variable set (or Dictionary put) block allows only changing value of the whole 1st level variable (or change contents of the main, 1st level dictionary, not the nested ones).

Henrik "The Developer" Lindqvist

unread,
May 30, 2023, 5:35:11 PM5/30/23
to Automate
Use the Dictionary put block, but first you need to extract it to a separate variable if it's "nested", e.g.:
  1. Variable set: modules = settings["modules"]
  2. Dictionary put: Dictionary= modules, Key= "check", Value= 1

MrGuy

unread,
Jun 1, 2023, 2:03:44 PM6/1/23
to Automate
I'll be using this soon too. Watching

MrGuy

unread,
Jun 4, 2023, 12:42:49 PM6/4/23
to Automate
Ok my turn. 
I store the json file with decode. Then I parse into dictionary. I can get the value of the nested key like this.

Variable = gameDictionary["gameName"]["mapSearch"]["coordinates"]["Xstart"]

Now I want to do like the author and write the values back to the file!

I'm trying to use dict put but can't figure out the syntax. I tried using gameDictionary with just the Xstart key but that didn't work. Next I tried the total path as above and without the dictionary name. 

gameDictionary["gameName"]["mapSearch"]["coordinates"]["Xstart"]

["gameName"]["mapSearch"]["coordinates"]["Xstart"]

Those didn't work either. How do I set the dictionary value for the nested Xstart key? I want these values stored in the json file afterwards. Which means I will then have reformat the dictionary back to the json text!

My json file contents:
{
 "gameName": {
  "appData": {
   "Display Name": "gameDisplayName",
   "Package Name": "gamePackageName",
   "Version Name": "gameVersionCodeNumberName",
   "Version Code": "gameVersionCodeNumber",
   "Cache Size": "gameVersionCacheSize",
   "Data Size": "gameDataSize",
   "Code Size": "gameCodeSize",
   "Apk Paths": "arrApkPathsrrApkPaths"
  },
  "mapSearch": {
   "coordinates": {
    "Xstart": 0,
    "Ystart": 1200,
    "Xend": 1200,
    "Yend": 0,
    "gridWidthPerRow": 300

MrGuy

unread,
Jun 4, 2023, 1:41:39 PM6/4/23
to Automate
ok using the thread below:

i was able to creat a dictionary for the specific json"dictionary" i was interested in.

gameMapSearchCoordinatesDicionary = gameDictionary["gameName"]["mapSearch"]["coordinates"]

them set the specifiv key value with dict put: 
gameMapSearchCoordinatesDicionary
Xstart
50

then when i print out the original ditionary key reference it has been updated!

gameDictionary["gameName"]["mapSearch"]["coordinates"]["Xstart"]

i get 50. Now i will creat the original json config with saved values as text and write to file!

Maybe there's a more elegant way to do this so please comment below if you know. Thanks!

P. Andreas Schmidt, IVE

unread,
Jun 4, 2023, 2:43:04 PM6/4/23
to Automate
No, this is how it has to be done, for the time being. I remember a post about this and Hendrik saying at least he was going to think about it or so, on how to make this possible in the block or so, but I couldn't find a post about this, nor can I find it on the planned features list...

​ ​man

unread,
Jul 16, 2023, 4:47:06 PM7/16/23
to Automate
Thank you for your reply! That looks like a quite elegant workaround :)
But please somebody confirm me one more thing. When I put a new value in the temporary "modules" variable, I still have to manually put* it in the general dictionary "settings", yes?  I assume there's no other way? 🤔
(Sorry, I'm a Javascriptist ;) )

*) Like this:
  1. Dictionary put: Dictionary= settings, Key= "modules", Value= modules

Henrik "The Developer" Lindqvist

unread,
Jul 17, 2023, 5:56:32 AM7/17/23
to Automate
No, since the temporary "modules" variable only holds a reference to the actual Dictionary. It's exactly how JavaScript works, e.g.
var modules = settings.modules;
modules["check"] = 1;
// not need to reassign settings.modules here
Message has been deleted

P. Andreas Schmidt, IVE

unread,
Jul 17, 2023, 11:17:38 AM7/17/23
to Automate
  Yeah, like Henrik says, if you just use the variable set block to that part of the dictionary to another variable, it's done by reference, not copying. So changing one changes the other and vv, because they look at the same memory slot. This means, you don't have to copy it back or so.
If you ever needed to have an actual separate copy, there's the copy function for - though I haven't understood yet what is the difference between deep and shallow copies. If you use this function, the new variable will be separate from the original, and any changes made to either, will only affect that one, and not the other.

​ ​man

unread,
Jul 17, 2023, 5:08:48 PM7/17/23
to Automate
That's lovely! 😍 Thank you!

Henrik "The Developer" Lindqvist

unread,
Jul 18, 2023, 5:23:16 AM7/18/23
to Automate
A "shallow copy" only duplicates the containing (root level) array or dictionary, not its (nested child) values.
A "deep copy" also duplicates every element value in an array and/or entry value in a dictionary, and any nested (child) values therein, and so on.

​ ​man

unread,
Jul 18, 2023, 6:11:35 AM7/18/23
to Automate
Oh, good to know. It would be nice if it was written in documentation of copy function :)
Reply all
Reply to author
Forward
0 new messages