How to loop through dynamically changing key elements in JSON

LinerSeven

Active member
Hello, All.

I would like to know about the "Iterate Dictionary/Array" action in Macrodroid.

For example, to JSON

JSON[tide][chart][2023-04-06]

and there is a dynamic key of type YYYY-MM-DD that changes with the passage of days.

In conclusion, the target key of the loop is,
JSON[tide][chart][{year}-{month_digit}-{dayofmonth}]
It would be better if it could be set like this,

However, the current MD does not allow Define Manually of the key and it is fixed.

Even if the element inside the key can be retrieved on the same day, the key will change the next day and the element cannot be retrieved.

The key changes the next day and the element cannot be retrieved.

How are you dealing with this?

Best Regards,
Liner Seven
 

MacroDroidDev

Administrator
Staff member
I'm not sure I understand the issue here. The iterate dictionary will not iterate sub-dictionaries so you will need to iterate the dictionary on :

JSON[tide][chart]

When doing this you should be able to use the magic text to access the value and key for each item with {iterator_value} and {iterator_dictionary_key}
 

Endercraft

Moderator (& bug finder :D)
I'm not sure I understand the issue here. The iterate dictionary will not iterate sub-dictionaries so you will need to iterate the dictionary on :

JSON[tide][chart]

When doing this you should be able to use the magic text to access the value and key for each item with {iterator_value} and {iterator_dictionary_key}
What I think @LinerSeven meant is that it's not possible to iterate dictionaries/arrays using magic text (I honestly don't remember if it's possible or not), like using the copy from dictionary/array doesn't support magic text.
 

LinerSeven

Active member
Dear Endercraft,

Thank you for your reply.

Could you please take a look at the attached macro?

When copying a JSON child element, I can copy it by specifying a direct key, but not by specifying a dynamic key.

As a result, I am stumped as I have no control over the loop target...

Best Regards,
Liner Seven
 

Attachments

  • Tide_20230406.macro
    151.5 KB · Views: 13

LinerSeven

Active member
In conclusion, I have dealt with this by converting the date part from the string before turning it into JSON to a simple string using a regular expression to force the key element to not change dynamically, but I think the problem is that you cannot manually specify the dictionary key in a loop.
 
Top