Jump to: navigation, search

Mistral/UsingYAQL

< Mistral
Revision as of 14:32, 4 March 2015 by Nikolay (talk | contribs) (YAQL Examples)

YAQL Examples

Prject github URL: https://github.com/ativelkov/yaql

Using YAQL it is possible to filter, process, access and pass data stored in JSON structure.

For example, this JSON will be used for showing some examples:

{
  "users": [
    {
       "tenant_id": "123",
       "username": "user1",
       "roles": ["admin"]
    },
    {
      "tenant_id": "321",
      "username": "user2",
       "roles": ["member"]
    },
    {
      "tenant_id": "123",
      "username": "user3",
       "roles": ["admin"]
    }
  ]
}

And suppose we just retrived this JSON from Task, or Action as its result. What we can do:

As the example, it is shown only publish section. The same with 'output' in Action.

Get number of users

publish: 
  users_count: <% $.users.length() %>

Function length() returns number of objects in container like it is done in major high-level programming languages.


Filter users with specific tenant_id

publish:
  tenant_123: <% $.users[$.tenant_id = '123'] %>

YAQL filters users with tenant_id == '123' and returns list containing only users 'user1' and 'user2'.


Get specific user

publish: 
  user2: <% $.users[$.username = 'user2'][0] %>

YAQL filters users with username == 'user2' and returns list containing only user 'user2' and we take the first one.