JsonObject.SelectToken(Text, var JsonToken) Method

Version: Available or changed with runtime version 1.0.

Selects a JsonToken using a JPath expression.

Syntax

[Ok := ]  JsonObject.SelectToken(Path: Text, var Result: JsonToken)

Parameters

JsonObject
 Type: JsonObject
An instance of the JsonObject data type.

Path
 Type: Text
A valid JPath expression.

Result
 Type: JsonToken
A JsonToken variable that will contain the result if the operation is successful.

Return Value

[Optional] Ok
 Type: Boolean
true if the read was successful; otherwise, false. If you omit this optional return value and the operation does not execute successfully, a runtime error will occur.

Remarks

The operation will fail if more or less than 1 tokens are the result of evaluating the JPath.

Example

The following example shows how to select a value from a complex JSON Object. We build up a select expression in the query variable, we select the token corresponding to the salary of the employee with the given employeeId, and finally, we convert the token to a Decimal value.

We assume that the company token contains JSON data similar to the one below.

{ "company": {
    "employees": [
      { "id": "Marcy",
        "salary": 8.95
      },
      { "id": "John",
        "salary": 7
      },
      { "id": "Diana",
        "salary": 10.95
      }
    ]
  }
}
local procedure SelectEmployeeSalary(companyData : JsonToken; employeeId : Text) salary : Decimal
var
    query : Text;
    salaryToken : JsonToken;
begin
    query := '$.company.employees[?(@.id=='''+employeeId+''')].salary';
    companyData.SelectToken(query, salaryToken);

    salary := salaryToken.AsValue().AsDecimal();    
end;

Note

Ensure that the selected expression contains ' (single quotation mark) and not " (double quotation mark) to decorate the string value.

JsonObject Data Type
Get Started with AL
Developing Extensions