DataEngine for .NET Standard | ComponentOne
Work with Data Engine / Connect Data Engine / Import Data from JSON File
In This Topic
    Import Data from JSON File
    In This Topic

    With the Data Engine library, you can import data from JSON files to the DataEngine table into a queryable collection of objects in your code.

    Note: For importing data from a JSON file into the DataEngine table, you need to add the Newtonsoft.Json NuGet package in your application, apart from the C1.DataEngine.Core and C1.DataEngine.Core.Api NuGet packages. 

    To connect Data Engine to a JSON file, you need to follow the steps given below:

    1. Initialize a new workspace folder relative to the project root directory using the Init method of the Workspace class.

      //Initialize a new workspace folder relative to the project root directory
      Workspace workspace = new Workspace();
      workspace.Init("workspace");
    2. Create a class containing properties that can hold the data corresponding to the fields you have in the JSON file.

      public class App
      {
          [JsonProperty("id")]
          public int AppId { get; set; }
          [JsonProperty("app_name")]
          public string AppName { get; set; }
          [JsonProperty("size_bytes")]
          public long AppSize { get; set; }
          [JsonProperty("price")]
          public decimal Price { get; set; }
          [JsonProperty("user_rating")]
          public float UserRating { get; set; }
          [JsonProperty("prime_genre")]
          public string Genre { get; set; }
      }
    3. Convert the JSON file data to a collection of custom .NET objects using the DeserializeObject method of the JsonConvert class, which is available in the Newtonsoft.Json NuGet package.

      //Convert JSON data to a collection of custom .NET objects
      List<App> appList = JsonConvert.DeserializeObject<List<App>>(File.ReadAllText("MobileAppStats.json"));
    4. To connect the DataEngine to the retrieved collection of custom objects, create an instance of the ObjectConnector class and pass the Workspace object and the custom collection initialized above, as parameters to its constructor. Use the ObjectConnector’s GetData method to create a DataEngine table containing the imported data. 

      // Import JSON data from custom collection to a DataEngine table             
      ObjectConnector<App> connector = new ObjectConnector<App>(workspace, appList);
      connector.GetData("MobileAppsStats");

      Once the connection is established, the queries can be defined and executed to fetch the desired data. Refer to Transform Data topic for more information.