First
The JSON path function ‘First’ is pretty similar to JSON path function At, except it is used to reference the first JSON value in JSON array or JSON collection (i.e., JSON array generated by applying JsonQL expressions, such as JSON path function ‘Where’, or ‘Flatten’).
The main difference between ‘At’ and ‘First’ is that ‘First’ has only one parameter criteria, as it does not require the other parameters used by ‘At’.
Function Parameters
- criteria:
Required: No
Type: lambda function of type “x => boolean”
Description: A criteria used to filter the result. If the parameter is not null, the first item matching criteria will be selected, rather than the first item in collection.
Example
Note
The following JSON files are referenced in JsonQL expressions in Example.json in example below: Data.json.
Example.json below demonstrates using First path function with JsonQL.
{
"Object1": {
"EvenNumbersArray": [
2,
4,
"Test",
[ 1, 3 ],
6,
10
],
"Array1": [
1,
2,
"Test",
{
"EmployeeId": 1
},
[ 5, "test", 20 ],
6,
13,
4
],
"EmptyArray": []
},
"Comment_Line1": "NOTE: Array indexers can be applied only to arrays, and not to collections resulted from using Where(), Flatten() and others,",
"Comment_Line2": "and the following expression will not work",
"Comment_Line3": "(Object1.Array1.Where(x => x != 1)[0]))",
"Comment_Line4": "and At(0) or First() function should be used instead as shown below",
"Get_First_Item_In_Result_Of_Where": "$value(Object1.Array1.Where(x => x != 1).First())",
"First_Using_Named_Parameters": "$value(Object1.Array1.Where(x => x != 1).First(criteria -> x => index > 1 && x >= 4))",
"GetFirstEmployeeWithSalaryGreaterThan_100000": "$value(parent.Object1.Companies.Flatten().Where(x => x.EmployeeId > 0 && x.Salary > 100000).First())",
"GetFirstItemInArray": "$(Object1.Array1.First())",
"Predicate_In_First_GetFirstItemInArray_GreaterThan_1": "$(Object1.Array1.First(x => x > 1))",
"Predicate_In_First_CompanyWithFirstEmployeeId_100": "$value parent.Object1.Companies.First(x => x.First(x => !HasField(x, 'Name')).EmployeeId == 100)"
}
The result (i.e., an instance of JsonQL.Compilation.ICompilationResult) is serialized to a Result.json file below.
Note
For brevity, the serialized result includes only serialized evaluated Example.json and does not include parent JSON files in JsonQL.Compilation.ICompilationResult.CompiledJsonFiles
{
"CompiledJsonFiles":[
{
"TextIdentifier": "Example",
"CompiledParsedValue":
{
"Object1": {
"EvenNumbersArray": [
2,
4,
"Test",
[
1,
3
],
6,
10
],
"Array1": [
1,
2,
"Test",
{
"EmployeeId": 1
},
[
5,
"test",
20
],
6,
13,
4
],
"EmptyArray": [
]
},
"Comment_Line1": "NOTE: Array indexers can be applied only to arrays, and not to collections resulted from using Where(), Flatten() and others,",
"Comment_Line2": "and the following expression will not work",
"Comment_Line3": "(Object1.Array1.Where(x => x != 1)[0]))",
"Comment_Line4": "and At(0) or First() function should be used instead as shown below",
"Get_First_Item_In_Result_Of_Where": 2,
"First_Using_Named_Parameters": 6,
"GetFirstEmployeeWithSalaryGreaterThan_100000": {
"EmployeeId": 3,
"Age": 75,
"Salary": 120000
},
"GetFirstItemInArray": "1",
"Predicate_In_First_GetFirstItemInArray_GreaterThan_1": "2",
"Predicate_In_First_CompanyWithFirstEmployeeId_100": [
{
"Name": "Company2"
},
{
"EmployeeId": 100,
"Age": 47,
"Salary": 110000
},
{
"EmployeeId": 102,
"Age": 65,
"Salary": 95000
},
{
"EmployeeId": 103,
"Age": 715,
"Salary": 98000
}
]
}
}
],
"CompilationErrors":
{
"$type": "System.Collections.Generic.List`1[[JsonQL.Compilation.ICompilationErrorItem, JsonQL]], System.Private.CoreLib",
"$values": []
}
}
The code snippet shows how the JSON file Example.json was parsed using JsonQL.Compilation.IJsonCompiler
// Set the value of jsonCompiler to an instance of JsonQL.Compilation.IJsonCompiler here.
// The value of JsonQL.Compilation.JsonCompiler is normally created by Dependency Injection container
// and it is normally configured as a singleton.
JsonQL.Compilation.IJsonCompiler jsonCompiler = null!;
var result = jsonCompiler.Compile(
new JsonTextData("Example", this.LoadExampleJsonFile("Example.json"),
new JsonTextData("Data", this.LoadExampleJsonFile("Data.json"))));