Pass unlimited number of parameters between tasks independently of order

Learn how to use JSON to pass an unlimited amount of structured data between tasks

  1. joaomgcd
    In Tasker you are natively limited to pass 2 parameters when calling a task. These will be assigned to %par1 and %par2 in the receiving task. This is very limited and will often prompt workarounds where you join multiple values in the calling task and then split them in the receiving task making it a mess to work with.

    With AutoTools Json Write you can now pass ANY number of variables or even a complex data structure from one task to another.

    Let me show you how easy it is ;)

    STEP 1 - CREATE TEST DATA


    (i) This step is just a quick way of creating test data. In a real situation your data can come from any place you wish. In this example we're going to send a list of cars (each with its brand, model and year) from one task to another.

    • Create a new Task and call it Send Cars
    • Add an AutoTools Arrays action
    • Set the Input Arrays field to Mercedes,Fiat,Ford|GLC,500,Focus|2015,1999,2004
    • Set the Output Names field to brand,model,year
    • Go back to the task and add a Flash action with the text
      • %brand()
      • %model()
      • %year()
    • Run the task and check that the data for the 3 cars shows up correctly.


    STEP 2 - WRITE JSON DATA


    • Add an AutoTools Json Write action
    • In the Simple Values section set
      • Json Keys to count
      • Json Values to %brand(#)
    (i) This will add a field in the root of the Json data named count which will contain the number of cars that we have (in this case 3).
    It'll look like this:
    Code (Text):

    {
        "count": "3"
    }
     
    • In the Arrays section set
      • Add to Array Key to cars
      • Array Object Keys to brand,model,year
      • Array Object Values to %brand()|%model()|%year()
    (i) Notice how AutoTools automatically adds ()| for you when you use the built-in variable selector
    (i) This will add all 3 cars to the JSON structure in one single action. The JSON data will look like this:
    Code (Text):

    {
        "count": "3",
        "cars": [
            {
                "brand": "Mercedes",
                "model": "GLC",
                "year": "2015"
            },
            {
                "brand": "Fiat",
                "model": "500",
                "year": "1999"
            },
            {
                "brand": "Ford",
                "model": "Focus",
                "year": "2004"
            }
        ]
    }
     
    • In the Output section enable the Prettify option so that the output is more readable
    • Go back to the task and add a Flash action with %atjsonresult
    • Run the task and check that the JSON structure above shows up


    STEP 3 - CALL RECEIVE CARS TASK


    • Add a Perform Task action
    • Set the Name field to Receive Cars
    • Set the Parameter 1 field to %atjsonresult
    (i) We're calling a task called Receive Cars (which we'll create next) with all the JSON data that we've created before. As you can see, you could've added any data you wanted and you'd still only use Parameter 1 to send it to the other task.


    STEP 4 - RECEIVE CARS


    • Create a new task and call it Receive Cars
    • Add an AutoTools Json Read action
    • Set the Json field to %par1
    (i) %par1 will contain whatever the calling task sends to this task in Parameter 1. In this case it'll be the cars JSON data.
    • Set the fields to model(),brand(),year(),count
    (i) Notice how the order isn't important here. You could've written these fields in any order and it would still work. This is very handy because calling tasks don't need to know which order the receiving task expects, it just needs to know which field names to use when sending the data.
    • Go back to the Task and add a Flash action with
      • %brand()
      • %model()
      • %year()
      • %count
    • Exit Tasker to save


    STEP 5 - TEST


    Go back to Tasker and run the Send Cars task. You'll see that first the flashes from the Send Cars task will show up and then the flash from the Receive Cars task will show up which shows that the data is intact and ready to use in the Receive Cars task! :cool: