Returning an Object From A Query Expression

zacharyc
Airkit
Published in
1 min readMar 24, 2022

Query Expression syntax is one of the unique features of Airscript. In Query Expression you are effectively iterating through a list and return some data. Until today, I thought the data returned was in array format. Today during our team coffee the question was posed, how do you get an Object out of a Query Expression.

It turns out there is a fun trick for this. Starting with some data that looks like this:

[
{
"key": "key1",
"value": "value1"
},
{
"key": "key2",
"value": "value2"
}
]

Running the following expression:

FROM el IN test SELECT "{{el.key}}": el.value

Returns:

{
"key1": "value1",
"key2": "value2"
}

This is useful for effectively joining multiple tables in Query Expressions (though I’m not going to show an example here).

Going in the reverse is also possible, to go from an object to an array. The trick is a useful Airscript function KEYS() which returns all the keys of an object.

FROM key 
IN KEYS(transform)
SELECT { "key": key, "value": transform[key] }

will return the original starting object.

--

--

zacharyc
Airkit
Writer for

programmer, coach, photographer, skier, snowboarder, sailor, hiker, biker, runner, gymnast