Is there a better way? - Recursive JSON Menu Parsing with Tasker actions
I Have the following JSON for a recursive menu. I used parts the Tasker Menu for this example.
I do realize this would be far easier to do with JavaScript, However I am trying to accomplish this by using Taskers JSON function and native Tasker actions alone.
The task I created works without issue and allows for easy additions and editing of the JSON.
My question is if I am missing a easier or better way to do this with just Tasker actions.
EDIT.... I also need to be able to list the menu items in any order, so a sub menu item could be listed in the middle of the reg action menu items.
The Current set up gets the main menu elements with a function like this.
``` %json[items.label]()
```
And displays them in a List Dialog action. If I select the third Menu item I retrieve the action to preform like this
``` %json[items.link](3)
```
When I select the forth menu item that has a sub menu. In that case the value of
``` %json[items.link](4) = "¥exit"
``` ¥exit is set as the the array name for the sub menu. So I now append ¥exit to my JSON function to get sub menu elements.
``` %json[items.¥exit.label]()
```
Am I missing an easier way to get the next array containing the sub menu?
``` [ { "items": [ { "label": "TaskerNet", "link": "action - Open TaskerNet" }, { "label": "Tutorials", "link": "action - Open Tutorials" }, { "label": "Disable Tasker", "link": "action - Disable Tasker" }, { "label": "Exit ->", "link": "¥exit", "¥exit": [ { "label": "Save First", "link": "action - save and close" }, { "label": "Cancel", "link": "action -Cancel exit" }, { "label": "Exit", "link": "action -exit no save" } ] }, { "label": "Preferences", "link": "action - Open Preferences" }, { "label": "Monitoring ->", "link": "¥mon", "¥mon": [ { "label": "Running Tasks", "link": "action - show running tasks" }, { "label": "Active Profiles", "link": "action - show Active Profile" }, { "label": "Run Log", "link": "action - show run log" } ] }, { "label": "Data ->", "link": "¥data", "¥data": [ { "label": "Clear", "link": "action - clear" }, { "label": "Backup", "link": "action -backup" }, { "label": "Restore ->", "link": "¥restore", "¥restore": [ { "label": "Auto Backup", "link": "action - auto backup" }, { "label": "User Backup", "link": "action -user backup" }, { "label": "Google Drive Backup", "link": "action -gd backup" } ] }, { "label": "share", "link": "action -share" }, { "label": "Description", "link": "action -Write to file" } ] } ] } ]
```
Task: working json menu Recursive test
A1: Variable Set [ Name: %json To: [ { "items": [ { "label": "TaskerNet", "link": "action - Open TaskerNet" }, { "label": "Tutorials", "link": "action - Open Tutorials" }, { "label": "Disable Tasker", "link": "action - Disable Tasker" }, { "label": "Exit ->", "link": "¥exit", "¥exit": [ { "label": "Save First", "link": "action - save and close" }, { "label": "Cancel", "link": "action -Cancel exit" }, { "label": "Exit", "link": "action -exit no save" } ] }, { "label": "Preferences", "link": "action - Open Preferences" }, { "label": "Monitoring ->", "link": "¥mon", "¥mon": [ { "label": "Running Tasks", "link": "action - show running tasks" }, { "label": "Active Profiles", "link": "action - show Active Profile" }, { "label": "Run Log", "link": "action - show run log" } ] }, { "label": "Data ->", "link": "¥data", "¥data": [ { "label": "Clear", "link": "action - clear" }, { "label": "Backup", "link": "action -backup" }, { "label": "Restore ->", "link": "¥restore", "¥restore": [ { "label": "Auto Backup", "link": "action - auto backup" }, { "label": "User Backup", "link": "action -user backup" }, { "label": "Google Drive Backup", "link": "action -gd backup" } ] }, { "label": "share", "link": "action -share" }, { "label": "Description", "link": "action -Write to file" } ] } ] } ] Structure Output (JSON, etc): On ]
A2: Variable Set [ Name: %depth To: items Structure Output (JSON, etc): On ]
<menu> A3: List Dialog [ Mode: Select Single Item Title: Menu Items: %json[%depth.label]() Button 1: Back Button 2: Quit Close After (Seconds): 120 First Visible Index: 0 ]
<button - Quit> A4: Stop [ ] If [ %ld_button eq Quit ]
<button - Back -> remove last key array name> A5: If [ %ld_button eq Back ]
A6: Array Set [ Variable Array: %depth Values: %depth Splitter: . ]
A7: Variable Set [ Name: %last_index To: %depth(#) Structure Output (JSON, etc): On ]
A8: Array Pop [ Variable Array: %depth Position: %last_index ] If [ %depth(#) > 1 ]
A9: Variable Set [ Name: %depth To: %depth(+.) Structure Output (JSON, etc): On ]
A10: Goto [ Type: Action Label Label: menu ]
A11: End If
<Item selected -> get sub menu or show action> A12: If [ %ld_selected Set ]
<value contains ¥ -> add value to JSON function to get sub menu> A13: If [ %json[%depth.link](%ld_selected_index) ~ *¥* ]
A14: Variable Set [ Name: %depth To: .%json[%depth.link](%ld_selected_index) Append: On Structure Output (JSON, etc): On ]
A15: Array Push [ Variable Array: %depth_index Position: 1 Value: 1 ]
A16: Goto [ Type: Action Label Label: menu ]
A17: End If
<show action linked to menu item> A18: Flash [ Text: Action is..
%json[%depth.link](%depth_index1) Tasker Layout: On Background Colour: #FFE22D2D Timeout: 60000 Text Colour: #FF14E539 Dismiss On Click: On Position: Top ]
A19: Goto [ Type: Action Label Label: menu ]
A20: Stop [ ]
A21: End If
A22: Goto [ Type: Action Label Label: menu ]
A23: Stop [ ]
https://taskernet.com/shares/?user=AS35m8lnbGhm%2F58jHvsiqVNumDAJZVkcfcE7gQxfcMjrFBCkp6sNKYf3YiK9WVWZBoDf&id=Task%3Aworking+json+menu+Recursive+test