Copy Fields ‘$copyFields’

  • The function ‘$copyFields’ allows merging the key/value combinations in an object evaluated by JsonQL expression that is passed to ‘$copyFields’ into another JSON object.

  • JsonQL will replace the key/value in JSON object with a string value which contains ‘$copyFields([JsonQL expression])’ with key/field pairs in JSON object generated from evaluating the JsonQL expression passed to ‘$merge’ items.

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

  • The key value in key/field pair in JSON object that uses a value containing “$copyFields(Json QL expression)” can use any key name (both key and value will be replaces with key/pair collection).

  • If the host JSON object has a key that matches a key generated by ‘$copyFields’ function and the key/value appears before the ‘$copyFields’ function, the key will be replaced with the key generated by ‘$copyFields’ function.

  • If the host JSON object has a key that matches a key generated by ‘$copyFields’ function and the key/value appears after the ‘$copyFields’ function, the key/value pair generated by ‘$copyFields’ function will be ignored, and the key/value in host JSON object will remain unchanged.

Example

Note

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

Example.json below demonstrates using $copyFields function.

{
  "Employees": [
    {
      "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
    }
  ],

  "CustomizedEmployee": {
    "Id": 1,
    "Title": "Project Manager",
    "Comments_fieldsCopiedFromEmployee": "'Id' field is also in Employees[1] and and field 'Id:1' will be replaced with value  from Employees[1]",
    "fieldsCopiedFromEmployee": "$copyFields(Employees[1])",
    "Comments_Address": "Address field was copied from Employees[1] but will be replaced with 'Address' field below, since  avalue is provided after fields were copied.",
    "Address": [ "Address 1", "Address 2" ],
    "Nationality": "US"
  },
  "CopyFieldsFromEmployeeInCompaniesJsonFile": {
    "Id": 1,
    "Title": "Project Manager",
    "Comments_fieldsCopiedFromEmployee": "'Id' field is also in copied empployee and field 'Id:1' will be replaced with value from selected employee",
    "fieldsCopiedFromEmployee": "$copyFields(Companies.Select(c => c.Employees).First())",
    "Comments_Address": "Address field was copied from delected employee but will be replaced with 'Address' field below, since  avalue is provided after fields were copied.",
    "Address": [ "Address 1", "Address 2" ],
    "Nationality": "UK"
  }
}

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":
      {
        "Employees": [
          {
            "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
          }
        ],
        "CustomizedEmployee": {
          "Title":  "Project Manager",
          "Comments_fieldsCopiedFromEmployee":  "'Id' field is also in Employees[1] and and field 'Id:1' will be replaced with value  from Employees[1]",
          "Id":  100000002,
          "Name":  "Alice Johnson",
          "Salary":  105000,
          "Age":  38,
          "Comments_Address":  "Address field was copied from Employees[1] but will be replaced with 'Address' field below, since  avalue is provided after fields were copied.",
          "Address": [
            "Address 1",
            "Address 2"
          ],
          "Nationality":  "US"
        },
        "CopyFieldsFromEmployeeInCompaniesJsonFile": {
          "Title":  "Project Manager",
          "Comments_fieldsCopiedFromEmployee":  "'Id' field is also in copied empployee and field 'Id:1' will be replaced with value from selected employee",
          "Id":  100000001,
          "Name":  "John Smith",
          "Salary":  99500,
          "Age":  45,
          "Comments_Address":  "Address field was copied from delected employee but will be replaced with 'Address' field below, since  avalue is provided after fields were copied.",
          "Address": [
            "Address 1",
            "Address 2"
          ],
          "Nationality":  "UK"
        }
      }
    }
  ],
  "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));