Variables
Variable declaration
$var: 123- simple Variable declaration$var.property: 123- variable with custom property declaration$var.["my-name"]: 123- variable with a custom property namemy-name.
Multiple variable declarations with different properties are additive however the language preference is to use an object declaration:
$var.prop1 = 123;
$var.prop2.child1 = 1;
$var.prop2.child2 = 2;
$var.prop2.child3 = 3;
Will Output:
{
"prop1": 123,
"prop2": {
"child1": 1,
"child2": 2,
"child3": 3
}
}
However the following declaration is equivalent and the preferred formatting for readability and performance reasons:
$var = {
prop1: 123;
prop2: {
child1 = 1;
child2 = 2;
child3 = 3;
}
}
Will Output:
{
"prop1": 123,
"prop2": {
"child1": 1,
"child2": 2,
"child3": 3
}
}
Variable Usage & Selection
Variables always behave like full JSON elements (values, objects or arrays). A variable or a selector will always return null if it can’t figure out the full path.
$var- value of the$varvariable.$var.prop1- value of theprop1property inside the$varvariable$var[0]- element0in the$vararray$var.array[0]- element0in thearrayproperty of the$varvariable
JSON Path Variable Selection
Full JSON Path selection is possible
by using the | select ( path ) modifier.
This can be used to select custom indexes from inside an array.
$result = $value | select( '$.items[ $index ]' )
Array Index selection
Arrays element can be selected from inside an array using the | at ( value ) modifier:
$array | at( 2 )- returns the 3-rd element (arrays are 0 indexed)$array | at( $index )- returns the$indexelement
Currently $array[ $index ] format is not supported.
Conditional Variable Selection
Variable arrays support conditional selection similar to the | filter ( condition ) modifier.
$array[( condition )]- select all items that respect a condition.$array[( $.name startsWith 'John' and $.id > 2 )]- select all elements where thenamestarts withJohnand have anid > 2