Value ‘$value’

  • The function ‘$value’ allows converting an expression used in JSON value of string type to a JSON value generated by JsonQL by evaluating the JSON expression parameter passed to function ‘$value’.

  • The resulted JSON value that replaces the original value can be any JSON value, such as string, boolean, number, JSON object, or JSON array.

  • The value of “$value(Json QL expression)” cannot be preceded or succeeded by any other text.

Example

Note

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

Example.json below demonstrates using $value function.

{
  "TestArray": [ 1, 3, 7, 9, 11, 13 ],

  "BooleanValueExample": "$value(TestArray[1] == 3 && Sum(TestArray) == 44)",
  "NumberValueExample": "$value(TestArray[1] + TestArray[2])",
  "StringValueExample": "$value(ToString(TestArray[1] + TestArray[2]))",
  "JsonObjectValueExample": "$value(Companies[0].CompanyData)",
  "JsonArrayValueExampleDescription": "Select first 3 employees across all companies",
  "JsonArrayValueExample": "$value(Companies.Select(c => c.Employees).Where(e => index <= 2))"
}

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":
      {
        "TestArray": [
          1,
          3,
          7,
          9,
          11,
          13
        ],
        "BooleanValueExample":  true,
        "NumberValueExample":  10,
        "StringValueExample":  "10",
        "JsonObjectValueExample": {
          "Name":  "Strange Things, Inc",
          "CEO":  "John Malkowich",
          "Address": {
            "Street":  "123 Maple Street",
            "City":  "Springfield",
            "State":  "IL",
            "ZipCode":  "62701"
          },
          "CountryDetails":  null
        },
        "JsonArrayValueExampleDescription":  "Select first 3 employees across all companies",
        "JsonArrayValueExample": [
          {
            "Id":  100000001,
            "Name":  "John Smith",
            "Address": {
              "Street":  "456 Oak Avenue",
              "City":  "Chicago",
              "State":  "IL",
              "ZipCode":  "60601"
            },
            "Salary":  99500,
            "Age":  45
          },
          {
            "Id":  100000002,
            "Name":  "Alice Johnson",
            "Address": {
              "Street":  "123 Maple Street",
              "City":  "New York",
              "State":  "NY",
              "ZipCode":  "10001"
            },
            "Salary":  105000,
            "Age":  38
          },
          {
            "Id":  100000003,
            "Name":  "Michael Brown",
            "Address": {
              "Street":  "789 Pine Lane",
              "City":  "Los Angeles",
              "State":  "CA",
              "ZipCode":  "90001"
            },
            "Salary":  89000,
            "Age":  50
          }
        ]
      }
    }
  ],
  "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 sharedExamplesFolderPath = new []
{
    "DocFiles", "MutatingJsonFiles", "Examples"
};

var companiesJsonTextData = new JsonTextData("Companies",
    LoadJsonFileHelpers.LoadJsonFile("Companies.json", sharedExamplesFolderPath));

var result = jsonCompiler.Compile(new JsonTextData("Example",
    this.LoadExampleJsonFile("Example.json"), companiesJsonTextData));