Data Management

Introducing the Expression Data Type

Summary

Xano's new expression data type is a game-changer, allowing you to write flexible inline expressions that can perform various operations and calculations. With expressions, you can streamline your development process and write more efficient code. In this guide, we'll walk you through the fundamentals of using expressions in Xano and provide real-world examples to help you get started.

Getting Started with Expressions

Expressions in Xano are versatile and can be used almost anywhere in the product. You can write expressions inside value boxes, enabling you to perform mathematical operations, reference inputs and variables, apply logical or conditional operations, and more – all within a single expression.

To use an expression, simply start typing your expression in the value box and select the "expression" data type from the dropdown menu.

Referencing Inputs and Variables

One of the core features of expressions is the ability to reference inputs and variables. To reference an input, use the `$input.inputName` syntax. For example, if you have an input named "score," you would reference it like this:

$input.score

You can then perform operations on the input value or combine it with other expressions.

Mathematical Operations

Expressions support a wide range of mathematical operators, such as addition (`+`), subtraction (`-`), multiplication (`*`), and division (`/`). Here's an example of adding 100 to the "score" input:

$input.score + 100

You can chain multiple operations together, like this:

$input.score - 50 + 3

String Concatenation

Expressions also allow you to concatenate strings using the tilde (`~`) symbol. For example, to prepend the word "score" to the input value:

"score" ~ " " ~ $input.score

Comparison Operators

Expressions provide a set of comparison operators, such as `==` (equals), `!=` (not equals), `>` (greater than), `<` (less than), `>=` (greater than or equal to), and `<=` (less than or equal to). These operators allow you to compare values and perform conditional operations.

$input.score == 50 // Returns true if score equals 50, false otherwise

Logical and Conditional Operators

Expressions support logical operators like `&&` (and), `||` (or), and `!` (not), which enable you to create complex conditional expressions. For example, to check if a score is between 10 and 100:

$input.score > 10 && $input.score < 100

You can use the ternary operator (`?:`) to return different values based on the result of a conditional expression:

$input.score > 10 && $input.score < 100 ? "yes" : "no"

This expression will return "yes" if the score is between 10 and 100, or "no" otherwise.

Using Filters in Expressions

One of the most powerful features of expressions is the ability to use Xano's built-in filters within your expressions. To use a filter, simply append a pipe character (`|`) followed by the filter name and its arguments (if any).

$input.score + 10 | add 20 // Adds 10, then 20 to the score

You can chain multiple filters together to perform complex operations.

Real-World Examples

Now that you understand the basics of expressions, let's explore a real-world example to solidify your understanding.

Imagine you have an API that returns customer data, including their ID, purchase history, location, and last seen date. Your goal is to determine whether a customer is considered a "high value" or "low value" based on their location, number of purchases, and total purchase value.

Here's how you could accomplish this using the traditional function stack approach:

  1. Call the external API to retrieve customer data.
  2. Use the `array.findFirstElement` function to find the customer based on the provided customer ID.
  3. Create a conditional statement that checks the following conditions:
  • The customer's location is "USA"
  • The count of the customer's purchase history is greater than 10
  • The sum of the customer's purchase values is greater than $5,000
  1. If all conditions are met, return "high value customer"; otherwise, return "low value customer."

While this approach works, it involves multiple steps and can become cumbersome as the logic grows more complex.

Now, let's see how we can achieve the same result using expressions:

$customerFound?.location == "USA" && $customerFound?.purchaseHistory.count() > 10 && $customerFound?.purchaseHistory.sum() > 5000 ? "high value customer" : "low value customer"

Here's a breakdown of this expression:

  1. `$customerFound` is the object returned from the `array.findFirstElement` function, containing the customer data.
  2. The `?.` operator is the safe navigation operator, which ensures the expression doesn't throw an error if `$customerFound` is `null` or `undefined`.
  3. The expression checks if the customer's location is "USA," the count of their purchase history is greater than 10, and the sum of their purchase values is greater than $5,000 – all using the `&&` operator.
  4. The ternary operator (`?:`) returns "high value customer" if the conditions are met, or "low value customer" otherwise.

By using expressions, you can consolidate the entire logic into a single, readable line of code, making it easier to maintain and understand.

Conclusion

Xano's expression data type is a powerful tool that empowers you to write concise, efficient code. By leveraging expressions, you can perform complex operations, apply conditional logic, and utilize Xano's built-in filters – all within a single inline expression.

As you continue to explore expressions, remember to refer to Xano's documentation for a comprehensive list of available operators and filters. Happy coding!

This transcript was AI generated to allow users to quickly answer technical questions about Xano.

Was this helpful?

I found it helpful

I need more support
Sign up for XanoSign up for Xano

Build without limits on a secure, scalable backend.

Unblock your team’s progress and create a
backend that will scale for free.

Start building for free