Filtering Data using Query Expression
Airscript includes a Query Expression syntax that allows you to operate on lists of items. This syntax looks somewhat similar to SQL but is based on a concept called Language Integrated Query.
##Query Expression Basics
Using this syntax you can filter or map a list of items.
The general syntax looks like the following:
In this simple example we go through a collection and filter out all items that have a score property greater than 80.
Any valid Airscript function is valid within our syntax. For example, below we use the Airscript STRING_FIND in the WHERE clause.
This example is going through all persons and finding all persons who have a name that contains Bob in it. Then we are returning the name of those persons. This will return as a list of p.name
While this is the basic example of our syntax here, we have even more functionality baked into our syntax. The supported technical arguments are:
Anything in brackets ([]) means that it is optional. We do not need to have a WHERE clause. There are also ORDER BY clause and LIMIT options. If you want your result to return only unique items you can put the DISTINCT modifier on your select and you receive a SET with duplicate items removed.
##Object Key Binding
When Filtering through an object rather than a list, you can access both parts of each key value pair within the object you iterate through. For example, consider the the first part of the following query.
In this query, key is a special reserved word that allows you to access keys in the JSON object you are iterating through. In the example above, the query would return a list of the keys in that object.
You can use this in various ways, including filtering by the keys themselves. Take for example the following object example.
Let's say I wanted to filter this object, and get an object that has everything but his "status." I would use the following query to filter by that key.
This would return
You can use this reserved keyword to manipulate objects just as you would with other Airscript functions. For example, you can use the Order By function to sort objects by their keys. Take the following example.
This query will return
Let's say I wanted to replace the key names within an object with something else. Using the same key reserved namespace, we could use Airscript to replace a key name with something else. For example
##Index Binding
One other important note: there is an index binding, meaning that you can also get the index of the item you are at in your iteration. For example:
In this case, index is a special reserved word that you put at the end of your expression and you can then refer to it in the rest of your statement. The result will be:
##Nested Query Expressions
The following example shows one way to nest a Query Expression within a Query Expression.
In this example we are pulling only qualified people out of a collection of people and then looking for people in that collection that have a score greater than 80.
Last updated