I am working on an ASP.NET Core project to migrate data from multiple JSON files into an SQLite database. Below is the DataMigration class I wrote for this purpose:
using System.Text.Json;namespace CargoHub{ public class DataMigration { private readonly AppDbContext _context; public DataMigration(AppDbContext dbContext) { _context = dbContext; } public async Task<List<string>> MigrateData(string folder = "../Data") { var processedFiles = new List<string>(); var jsonFiles = Directory.GetFiles(folder); foreach (var jsonFile in jsonFiles) { try { await ProcessFile(jsonFile); processedFiles.Add(jsonFile); } catch (Exception ex) { Console.WriteLine($"Error processing {jsonFile}: {ex.Message}"); } } return processedFiles; } private async Task ProcessFile(string filePath) { string fileName = Path.GetFileNameWithoutExtension(filePath); switch (fileName) { case "clients": var clients = await DeserializeJson<List<Client>>(filePath); await SaveToDatabase(clients); break; // Uncomment to handle more file types // case "inventories": // var inventories = await DeserializeJson<List<Inventory>>(filePath); // await SaveToDatabase(inventories); // break; case "item_groups": var itemGroups = await DeserializeJson<List<ItemGroup>>(filePath); await SaveToDatabase(itemGroups); break; } } private async Task<List<T>> DeserializeJson<T>(string filePath) { try { var jsonData = await File.ReadAllTextAsync(filePath); return JsonSerializer.Deserialize<List<T>>(jsonData)!; } catch (Exception ex) { Console.WriteLine($"Error deserializing {filePath}: {ex.Message}"); return new List<T>(); } } private async Task SaveToDatabase<T>(List<T> entities) where T : class { if (entities.Count == 0) return; _context.AddRange(entities); int changedRows = await _context.SaveChangesAsync(); Console.WriteLine(changedRows); } }}
Here's the Client model that represents the structure of the clients.json file:
using System.Text.Json.Serialization;namespace CargoHub{ public class Client : BaseModel { [JsonPropertyName("id")] public int Id { get; set; } [JsonPropertyName("name")] public string? Name { get; set; } [JsonPropertyName("address")] public string? Address { get; set; } [JsonPropertyName("city")] public string? City { get; set; } [JsonPropertyName("zip_code")] public string? ZipCode { get; set; } [JsonPropertyName("province")] public string? Province { get; set; } [JsonPropertyName("country")] public string? Country { get; set; } [JsonPropertyName("contact_name")] public string? ContactName { get; set; } [JsonPropertyName("contact_phone")] public string? ContactPhone { get; set; } [JsonPropertyName("contact_email")] public string? ContactEmail { get; set; } }}
The BaseModel class looks like this:
namespace CargoHub{ public class BaseModel { [JsonPropertyName("created_at")] public DateTime? CreatedAt { get; set; } [JsonPropertyName("updated_at")] public DateTime? UpdatedAt { get; set; } }}
Here is an example of the clients.json file I am trying to process:
[ {"id": 1,"name": "Raymond Inc","address": "1296 Daniel Road Apt. 349","city": "Pierceview","zip_code": "28301","province": "Colorado","country": "United States","contact_name": "Bryan Clark","contact_phone": "242.732.3483x2573","contact_email": "robertcharles@example.net","created_at": "2010-04-28 02:22:53","updated_at": "2022-02-09 20:22:35" }, {"id": 2,"name": "Williams Ltd","address": "2989 Flores Turnpike Suite 012","city": "Lake Steve","zip_code": "08092","province": "Arkansas","country": "United States","contact_name": "Megan Hayden","contact_phone": "8892853366","contact_email": "qortega@example.net","created_at": "1973-02-24 07:36:32","updated_at": "2014-06-20 17:46:19" }]
The Issue
Even though I named all the properties in my Client class to match the JSON structure, I still encounter issues when deserializing the data. Specifically, some fields are not being populated correctly.
I have already tried removing the [JsonPropertyName] attributes, but the problem persists.
What could I be missing in the deserialization process, or is there something else wrong with my approach?
Any help would be appreciated!