How to conditionally select first entry from one of two dictionaries?

narthur

New member
Noob here. I'm making two HTTP requests and saving the results to variables. Both requests return a JSON array of objects, which I then parse, resulting in two separate dictionary variables. So something like this:

first_dict = {
0: {
important_key: 3333
},
...
}

second_dict = {
0: {
important_key: 4444
},
...
}

I need to create a new variable that contains the first entry from one of the dictionaries, depending on which of the two candidates has the lowest number for `important_key`. How would I go about doing this?

I thought about merging the two dictionaries and sorting them by that key, but was unclear how to do that. I also thought about creating two new variables, each containing the first entry in its respective dictionary, and then trying to use a conditional to select between them, but again I'm so green I'm having trouble figuring out how to do that.

Help anyone?
 

Endercraft

Moderator (& bug finder :D)
If first_dict[important_key] < [lv=second_dict[important_key]] then :
Set variable use_first to true
else :
Set variable use_first to false

I believe this should work.
 

narthur

New member
Hmm, ok, trying to make this work. I'm adding a conditional, used first_dict[0][important_key] as the variable, selected less than, and now I'm trying to input this as an expression for the comparison:

[lv=second_dict[0][important_key]]

But the OK button is greyed out, so I assume I'm still doing something wrong. Any idea what it might be?
 

narthur

New member
@Endercraft Hmm, that's not working either unfortunately.

@余川705

Google Translate: Gray is because the data in the JS parsing is a string, and you need to convert to integer or decimal if you want to compare sizes

Oh, hmm, that might make sense... So if the input was this:

`[{'important_key': 3333}]`

That would still result in `important_key` getting parsed out as a string?

EDIT: I just tried setting a new variable with type integer using the following expressions:

[lv=second_dict[0][important_key]]
[lv=second_dict[0[important_key]]]
second_dict[0][important_key]
second_dict[0[important_key]]

In all cases the ok button was grayed out.
 
Last edited:

余川705

New member
[ USER = 4192 ] @ Endercraft [/USER ]嗯,不幸的是,这也不起作用。

@余川705

Google Translate:Gray是因为JS解析中的数据是字符串,如果要比较大小,需要转换为整数或十进制

哦,嗯,也许有道理...如果输入是这样的:

[ { ' important _ key ':3333 } ]

这仍然会导致' important _ keykey被解析为字符串?

编辑:我刚刚尝试使用以下表达式设置一个integer类型的新变量:

[lv=second_dict[0][important_key]]
[lv=second_dict[0[important_key]]]
second_dict[0][important_key]
second_dict[0[important_key]]

在所有情况下,确定按钮都是灰色的。
js得到的就算是纯数字格式也是字符串,转为整数或者十进制用函数MIN()不就比较出来了
 

Attachments

  • Screenshot_2022-10-17-00-40-20-787_com.arlosoft.macrodroid.jpg
    Screenshot_2022-10-17-00-40-20-787_com.arlosoft.macrodroid.jpg
    200.2 KB · Views: 8

Endercraft

Moderator (& bug finder :D)
You can also set a string var (example : str1) to [lv=first_dict[0[important_key]]] and convert it in an integer var (int1) by using [strval=str1], then do the same for [lv=second_dict[0[important_key]]].
 
Top