AutoTools Json Read - Getting Started

Learn the ins and outs of the Json Read action in AutoTools with several basic examples

  1. joaomgcd
    In this tutorial you're going to learn how to get started with reading JSON in Tasker using the AutoTools Json Read action.

    The Json Read action is the simplest way ever of reading JSON in Tasker as you'll see below.

    Table Of Contents

    If you want to know more about JSON, read this or google what is json for more info.

    This tutorial will be based on the JSON data available at this URL:
    https://joaoapps.com/AutoApps/Help/Info/com.joaomgcd.autotools/test.json

    The JSON object in the file is the following:
    Code (Text):

    {
        "kindergarten": {
            "children": [
                {
                    "name": "Maria",
                    "age": 7,
                    "toys": 4,
                    "body": {
                        "height": "130",
                        "weight": "20"
                    }
                },
                {
                    "name": "Janet",
                    "age": 6,
                    "toys": 2,
                    "body": {
                        "height": "100",
                        "weight": "25"
                    }
                },
                {
                    "name": "Siri",
                    "age": 8,
                    "body": {
                        "height": "170",
                        "weight": "30"
                    }
                },
                {
                    "name": "Chalice",
                    "age": 5,
                    "toys": 6,
                    "body": {
                        "height": "150",
                        "weight": "40"
                    }
                },
                {
                    "name": "Alice",
                    "age": 7,
                    "toys": 22,
                    "body": {
                        "height": "110",
                        "weight": "46"
                    }
                }
            ],
            "toyboxes": [
                {
                    "name": "Corner",
                    "toys": 10
                },
                {
                    "name": "Main",
                    "toys": 54
                },
                {
                    "name": "Hallway",
                    "toys": 5
                }
            ]
        }
    }
     
    This JSON basically describes a kindergarten.
    The kindergarten has children and toyboxes.
    A child has a name, an age, number of toys and a physical description named body.
    A child's body has 2 properties: height and weight
    A toybox has a name and a number of toys.

    To sum it up, this is the basic structure of a kindergarten:
    • kindergarten
      • children
        • name
        • age
        • toys
        • body
          • height
          • weight
      • toyboxes
        • name
        • toys
    Try to correlate this structure to the JSON above to understand how the data is presented in the JSON. :)

    First, lets get the JSON data from the aforementioned URL and store it locally in Tasker so we can use it a bunch of times.

    STEP 1 - GETTING JSON TEXT AND STORING IT ALL


    (i) This will create a variable with all the JSON content from the file so that we can re-use it as many times as we like
    • Go back to Tasker and add a Flash action with the text %json.
    • Run the task to see all the JSON content.



    Ok, let's start with the easiest possible read operation: let's get a child's name:

    STEP 2 - GETTING FIRST NAME FROM LIST


    • Add another AutoTools Json Read action
    • Set the Json field to %json
    • Set the Fields field to name
    (i) Notice how Simple Mode is selected by default. This means that you can simply write a JSON field's name and AutoTools will automatically look for a field with that name in all the JSON data and retrieve the first value it finds. In this case, this will return the first name found in the JSON data, which is the first child's name: Maria.
    (i) Notice how toyboxes also have name fields but since they appear later in the structure AutoTools didn't check them first.
    • Go back to the task and add a Flash action with the text %name
    • Disable the first Flash action
    • Run the task and see how it flashes Maria



    Getting a name is fine and dandy, but we want to get all names! Simple:
    (Note: you can also get multiple fields in the same actions as Step 11 in this tutorial shows)

    STEP 3 - GETTING ALL NAMES FROM LIST


    • Copy the second AutoTools Json Read action in the task and paste it as the last element
    • Disable the previous 2 actions
    (i) I wanted to keep all the "old" actions as we go along so that you can go back to them anytime you want to revisit something you did before you case you need to understand it better, or just see it in action again.
    • Edit the new action
    • Edit the Fields field and add () at the end, making the field name()
    (i) By adding () at the end of a field you're telling AutoTools that you want all values of fields called name and not just the first one it finds.
    • Go back to Tasker and add a Flash action with the text %name()
    • Run the task and notice how all names show up
    (i) %name() is an array so you can access individual values in the array. For example, if you access %name(1) you'll get the fist name, if you access %name(2) you'll get the second name, etc. More about Tasker arrays here.



    "Hold on there smarty pants! I don't want ALL names. I just wanted the children's names!" - well, that's what you get by using simple mode :p. Simple mode will look everywhere for the fields you want. It's simpler to use but it may not be what you want in every situation.
    Let's take a look at how you can get only the children's names:

    STEP 4 - GETTING JUST THE CHILDREN'S NAMES


    • Paste the previously copied AutoTools Json Read action again
    • Disable Simple Mode
    • Edit the Fields field and set it to kindergarten.children.name()
    (i) As you can see, with Simple Mode disabled you need to give AutoTools the full path to the field you want to get. Separate each part of the path with a . character. With Simple Mode disabled you can also use () at the end to get multiple results, or if you don't use it you'll only get the first result as before.
    • Go back to Tasker and add a Flash action with %kindergarten_children_name()
    (i) Since Tasker variables can't have dots in their names, AutoTools replaces dots with underlines.
    • Run the task and check how only the children's names will show up



    "What? That's a really ugly variable name! I want my %name() back!!" - well you're in a good mood today, aren't you? :rolleyes:
    We're not simply going to call it %name() because toyboxes also have names, so let's call it %childrenname(), ok?

    STEP 5 - RENAMING OUTPUT VARIABLES


    • Edit the last AutoTools Json Read action.
    • Go to Advanced
    • Set the Variable Name field to childrenname()
    (i) Don't forget to use the () at the end so that AutoTools knows you want multiple results!
    • Go back to Tasker and set the text in the Flash action to %childrenname()
    • Run the task and check that the names of all children still show up



    Ok, so we got all the children's names. Now we can also get all the toybox names. Can you guess how? :D

    STEP 6 - GETTING JUST TOY BOX NAMES


    • Copy the last 2 AutoTools Json Read and Flash actions and paste them as the last 2 actions in the task.
    • Disable the previous 2 AutoTools Json Read and Flash actions
    • Edit the AutoTools Json Read action
    • Replace children with toyboxes in the Fields name so that the value is set to kindergarten.toyboxes.name()
    • Go to Advanced and set the Variable Name field to boxname()
    • Go back to Tasker and edit the Flash action so that the text is %boxname()
    • Run the task and check how the box names show up



    You can also get the name of any particular child you may want. For example, you can get the name of the first child by using [0] where 0 is the position of the element you want.
    If you're not into programming then it may be a bit confusing why 0 corresponds to the first position, but that's how it works most of the times. 0 is the first position, 1 is the second, 2 is the third and so on.
    You can read more about it here.

    STEP 7 - GETTING FIRST CHILD'S NAME


    • Disable the last 2 actions in the task
    • Paste the 2 previously copied AutoTools Json Read and Flash actions again.
    • Edit the AutoTools Json Read action
    • Edit the Fields option, add [0] after children and remove the () at the end to make it kindergarten.children[0].name
    (i) As you can see, we applied the [0] to the children list. In JSON, a list is contained between [ ]. As you can see in the JSON structure above, it goes like "children": [ ............. ]
    • Go to Advanced and set the Variable Name value to firstchildname
    • Go back to Tasker and set the Flash action's text to %firstchildname
    • Run the task and check that the first child's name shows up



    "Ok, enough with the names! What else can this do?" - well, you can do some math too :cool:
    For example, lets get the age average for this kindergarten. Sounds complicated? It's rather easy!

    STEP 8 - GETTING CHIDLREN'S AGE AVERAGE


    • Disable the last 2 actions in the task
    • Paste the 2 previously copied AutoTools Json Read and Flash actions again.
    • Edit the AutoTools Json Read action
    • In the Calculations section set Average to kindergarten.children.age
    (i) Don't use the () here because you're only getting back 1 value for all the children: the average age of all the children
    • Go back to Tasker and edit the enabled Flash action.
    • Add the text %kindergarten_children_age_average to it
    • Run the Task and check how the age average is 6.6



    You can also easily calculate the sum of some values. For example, lets find out how many toys are in all the toyboxes combined:

    STEP 9 - GETTING TOTAL NUMBER OF TOYS IN TOYBOXES


    • Disable the last 2 actions in the task
    • Copy-paste the AutoTools Json Read and Flash actions in A9 and A10 (just because these already read the toybox names).
    • Edit the AutoTools Json Read action
    • In the Calculations section set Sum to kindergarten.toyboxes.toys
    • Go back to Tasker, edit the Flash action and add %kindergarten_toyboxes_toys_sum to it
    • Run the task and check how the sum of toys in the toyboxes is 69



    If you want you can even get the names and sum of toys for toyboxes that have more than X number of toys:

    STEP 10 - GETTING BOXES WITH MORE THAN 5 TOYS


    • Edit the last AutoTools Json Read action
    • In the Filter section set the Filter Fields value to kindergarten.toyboxes.toys
    • Set the Filter Values field to >5
    (i) This will make AutoTools ignore all toy boxes with 5 toys or less.
    • Go back to Tasker, run the task again and check how only 2 toy box names are returned now and that the sum is now 64



    Last but not least we can even sort the results. Let's order the toy boxes by number of toys from the one that has the most amount of toys to the one that has the least:

    STEP 11 - SORTING BY TOY COUNT


    • Edit the last AutoTools Json Read action
    • In the Sorting section set the Sort Array Key to kindergarten.toyboxes and the Sort Array Object Key to toys
    (i) As you can see in the JSON stucture, kindergarten.toyboxes is an array (as shown by the [ ]) and toys is one of the fields present in the toybox elements in that array
    • Edit the Fields field and set it to kindergarten.toyboxes.name(),kindergarten.toyboxes.toys()
    • Go to Advanced, edit the Variable Name field and set it to boxname(),toys()
    (i) As you can see you can also get multiple JSON fields in the same action
    • Go back to Tasker and add %toys() to the Flash action
    • Run the task to see how the names and number of toys are now sorted from biggest to smallest number of toys.
    (i) In the original JSON structure you can see that the order is Corner,Main but in this action it was returned as Main,Corner because of the sorting that was done.



    Of course, reading kindergarten data is not going to be very handy in most cases, so go out and find some interesting JSON data to read!
    For example:
    • Get any page on Reddit in a JSON structure by adding .json at the end of the URL: example
    • Get info about an artist via the Spotify API: example

    Hope this solves all the setup and hard-work that parsing JSON usually entails in Tasker once and for all! :cool:

Recent Reviews

  1. TotallyNoob
    TotallyNoob
    5/5,
    Very useful for noob like me. I successfully use it for a project.
  2. Suhel
    Suhel
    5/5,
    Excellent reference guide
  3. ilias
    ilias
    5/5,
    This was exactly what I needed to get started parsing Json in Tasker. Thanks alot!
  4. KaszasM
    KaszasM
    5/5,
    cool