Dot Notation & Sub Path
Last updated
Last updated
Dot notation allows you to access specific fields or data nested within a variable. Dot notation is a general programming concept and it is also a useful tool in Xano. You can apply dot notation on any input/value line within the Function Stack or Response - whether that be within Functions or Filters. Watch the tutorial to understand important rules and concepts around dot notation in Xano.
Dot notation must be used on an existing variable in Xano.
Let's use the variable car
as an example. Below you can see that 'car' is data retrieved from a database table with a Get Record function. The response shows us the contents of the variable - all the fields and data.
Let's say we want to retrieve just the make
of the car
variable. For this we can use dot notation to do so.
**It's very important to make sure the data type, as signified on the top of the input/value line is var: any (this signifies we have a variable data type selected).**
Once the variable is selected, add a '.' after the variable name and type out the field name of the data you wish to retrieve. **Spelling must be an exact match**
If we return the new variable make of car
we will return the value of car.make
, which is "toyota."
Using the same example from above, let's say we have a Query all records function that queries all records from the car database table and returns an array of 3 cars stored in the variable car
. It will return a large payload that looks something like below:
Now if we only wanted to return a list of car makes without all the extra bloat, traditionally you would have to do a for each loop function and store each car name in an array in its own variable. Luckily, using Xano's Dot Notation engine, you can easily get the makes of the cars.
We can create a variable and specify car.make
which will return an array of just the car makes:
Now when you return the variable just_the_makes
It will return an array that looks like this:
The GET filter allows you to return the value of an object at a specified path. In other words, it's another way to use dot notation but with a filter.
You can think of the GET filter like replacing a .
in dot notation. For example, to retrieve the make of a car instead of car.make
- you could apply the GET filter to say car|GET|make
The GET filter has a unique advantage that can be very useful for certain use cases. It allows you to define a default value in case the specified path does not exist.
Use cases where this may come in handy would be when calling an external API or receiving a Webhook and the returned payload can differ. Possibly you are working with a field that is sometimes present and sometimes not. Defining a default value with the GET filter allows your function stack to continue to run and complete the logic you are performing. You can also use conditional logic based on a default value to perform certain logic if the specified field does not exist.
Xano can automatically build your dot notation for you using the Sub Path feature.
For this example, let's take the following JSON object.
If we wanted to get the title of the first book in a new variable, we would use the following subpath, assuming our variable is named data
As data structures become more complex, building this sub pathing manually can be tedious. Xano can help you auto-complete this data, making it as easy as possible. Just run your function stack to populate the autocomplete, and choose the Sub Path feature next to your variable names when referencing them. Check out the interactive tutorial below to see how it works.
The GET filter also supports inline expressions for even more powerful data retrieval. See more here.
The path option is parsed using the Xano Expression syntax. In some rare cases, the path you are retrieving may resemble an expression, such as event[0]. If this is the case, simply wrap your path in [" and "] to ensure it behaves as expected.
While quite rare, there are occasions when a . may be required in a path name. An example of this is likely to come from a 3rd party API that requires a . in a parameter name (i.e. parameter = name.first). Standard JSON and dot notation would treat "first" as a subpath of "name", however, in this rare example "name.first" is the entire name of the path.
To satisfy this use-case, Xano implements an escape sequence to tell the API that the . is required in the path name by implementing a double dot or .. where required.
For this example, we will implement the path name "first.name" with the escape sequence.
The result is: