At

  • The JSON path function ‘At’ is used to reference a JSON value at specific index in JSON array or JSON collection (i.e., JSON array generated by applying JsonQL expressions, such as JSON path function ‘Where’, or ‘Flatten’).

Note

NOTE: Array indexers described in section Array Indexers can be applied only to arrays, and not to collections resulted from using Where(), Flatten() and others. At path function can be applied in these cases if necessary (multiple chained applications of At can be used).

Function Parameters

  • index:
    • Required: Yes

    • Type: number

    • Description: Index of selected item

  • criteria:
    • Required: No

    • Type: lambda function of type “x => boolean”

    • Description: A criteria used to filter the result, before selecting an element with index specified in parameter index .

  • isReverseSearch:
    • Required: No

    • Type: boolean

    • Description: Indicates if search should be done in reverse order.

Example

Note

The following JSON files are referenced in JsonQL expressions in Example.json in example below: Data.json.

Example.json below demonstrates using At 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 != 2)[2]))",
  "Comment_Line4": "and At(2) function should be used instead as shown below",
  "Get_Third_Item_In_Result_Of_Where": "$value(Object1.Array1.Where(x => x != 2).At(2))",

  "At_Using_Named_Parameters": "$value(Object1.Array1.At(index -> 2, criteria -> x => index > 0, isReverseSearch -> false))",

  "GetFirstEmployeeWithSalaryGreaterThan_100000": "$value(parent.Object1.Companies.Flatten().Where(x => x.EmployeeId > 0 && x.Salary > 100000).At(0))",
  "GetThirdEmployeeWithSalaryGreaterThan_100000": "$value(parent.Object1.Companies.Flatten().Where(x => x.EmployeeId > 0 && x.Salary > 100000).At(2))",
  "GetThirdItemInArray": "$value(Object1.Array1.At(3))",

  "GetCompanyWithFirstEmployeeId_100_UsingAt_1": "$value(parent.Object1.Companies.Where(x => x.At(1).EmployeeId == 100).First())",
  "GetCompanyWithFirstEmployeeId_100_UsingAt_2": "$value(parent.Object1.Companies.First(x => x.At(1).EmployeeId == 100))",
  "GetCompanyWithFirstEmployeeId_100_UsingIndex": "$value(parent.Object1.Companies.Where(x => x[1].EmployeeId == 100).First())",

  "UseArrayIndexerOnResultOfAt": "$value(Object1.Array1.Where(x => x != 2).At(3).[1])",

  "PredicateWith_At": "$value(Object1.Array1.At(2, x => x > 2))",
  "PredicateWith_And_ReverseSearch_With_At": "$value(Object1.Array1.At(2, x => x != 4, true))",
  "ReverseSearch_With_At": "$value(Object1.Array1.At(2, isReverseSearch -> true))"
}

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 != 2)[2]))",
        "Comment_Line4":  "and At(2) function should be used instead as shown below",
        "Get_Third_Item_In_Result_Of_Where": {
          "EmployeeId":  1
        },
        "At_Using_Named_Parameters": {
          "EmployeeId":  1
        },
        "GetFirstEmployeeWithSalaryGreaterThan_100000": {
          "EmployeeId":  3,
          "Age":  75,
          "Salary":  120000
        },
        "GetThirdEmployeeWithSalaryGreaterThan_100000": {
          "EmployeeId":  201,
          "Age":  39,
          "Salary":  103000
        },
        "GetThirdItemInArray": {
          "EmployeeId":  1
        },
        "GetCompanyWithFirstEmployeeId_100_UsingAt_1": [
          {
            "Name":  "Company2"
          },
          {
            "EmployeeId":  100,
            "Age":  47,
            "Salary":  110000
          },
          {
            "EmployeeId":  102,
            "Age":  65,
            "Salary":  95000
          },
          {
            "EmployeeId":  103,
            "Age":  715,
            "Salary":  98000
          }
        ],
        "GetCompanyWithFirstEmployeeId_100_UsingAt_2": [
          {
            "Name":  "Company2"
          },
          {
            "EmployeeId":  100,
            "Age":  47,
            "Salary":  110000
          },
          {
            "EmployeeId":  102,
            "Age":  65,
            "Salary":  95000
          },
          {
            "EmployeeId":  103,
            "Age":  715,
            "Salary":  98000
          }
        ],
        "GetCompanyWithFirstEmployeeId_100_UsingIndex": [
          {
            "Name":  "Company2"
          },
          {
            "EmployeeId":  100,
            "Age":  47,
            "Salary":  110000
          },
          {
            "EmployeeId":  102,
            "Age":  65,
            "Salary":  95000
          },
          {
            "EmployeeId":  103,
            "Age":  715,
            "Salary":  98000
          }
        ],
        "UseArrayIndexerOnResultOfAt":  "test",
        "PredicateWith_At":  4,
        "PredicateWith_And_ReverseSearch_With_At": [
          5,
          "test",
          20
        ],
        "ReverseSearch_With_At":  6
      }
    }
  ],
  "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"))));