using System.Collections.Generic;
using Newtonsoft.Json.Serialization;
public static void Main()
""wh_loc"" : ""Atlanta"",
""item_sku"" : ""WIDG10"",
""item_desc"" : ""Widget"",
""item_sku"" : ""THGY20"",
""item_desc"" : ""Thingy"",
""item_sku"" : ""DOOD30"",
""item_desc"" : ""Doodad"",
""item_sku"" : ""GAD40"",
""item_desc"" : ""Gadget"",
""item_sku"" : ""WHAT01"",
""item_desc"" : ""Whatsit"",
var resolver = new DynamicMappingResolver();
resolver.RenameProperty<Warehouse>(nameof(Warehouse.Id), "wh_id");
resolver.RenameProperty<Warehouse>(nameof(Warehouse.Location), "wh_loc");
resolver.RenameProperty<Warehouse>(nameof(Warehouse.Items), "wh_item_list");
resolver.RenameProperty<Item>(nameof(Item.Id), "item_id");
resolver.RenameProperty<Item>(nameof(Item.StockNumber), "item_sku");
resolver.RenameProperty<Item>(nameof(Item.ShortDescription), "item_desc");
resolver.RenameProperty<Item>(nameof(Item.Price), "item_price");
resolver.RenameProperty<Item>(nameof(Item.QuantityInStock), "item_qty");
var settings = new JsonSerializerSettings { ContractResolver = resolver };
var list = JsonConvert.DeserializeObject<List<Warehouse>>(json, settings);
Console.WriteLine($"Item Warehouse Stock No Description Price Qty");
Console.WriteLine($"---- --------- -------- ----------- ------ ----");
foreach (var warehouse in list)
foreach (var item in warehouse.Items)
Console.WriteLine($"{item.Id,-4} {warehouse.Location,-9} {item.StockNumber,-8} {item.ShortDescription,-11} {item.Price,6:N2} {item.QuantityInStock,4:N0}");
resolver = new DynamicMappingResolver();
resolver.RenameProperty<Warehouse>(nameof(Warehouse.Id), "warehouse");
resolver.RenameProperty<Warehouse>(nameof(Warehouse.Location), "city");
resolver.RenameProperty<Warehouse>(nameof(Warehouse.Items), "inventory");
resolver.RenameProperty<Item>(nameof(Item.Id), "id");
resolver.RenameProperty<Item>(nameof(Item.StockNumber), "sku");
resolver.RenameProperty<Item>(nameof(Item.ShortDescription), "name");
resolver.RenameProperty<Item>(nameof(Item.Price), "price");
resolver.RenameProperty<Item>(nameof(Item.QuantityInStock), "qty");
settings = new JsonSerializerSettings { ContractResolver = resolver, Formatting = Formatting.Indented };
json = JsonConvert.SerializeObject(list, settings);
public int Id { get; set; }
public string Location { get; set; }
public List<Item> Items { get; set; }
public int Id { get; set; }
public string StockNumber { get; set; }
public string ShortDescription { get; set; }
public decimal Price { get; set; }
public int QuantityInStock { get; set; }
public class DynamicMappingResolver : DefaultContractResolver
private Dictionary<Type, Dictionary<string, string>> _propertyMappingsByType = new Dictionary<Type, Dictionary<string, string>>();
public void RenameProperty<T>(string memberName, string jsonName)
if (!_propertyMappingsByType.TryGetValue(typeof(T), out Dictionary<string, string> mappingsForType))
mappingsForType = new Dictionary<string, string>();
_propertyMappingsByType.Add(typeof(T), mappingsForType);
mappingsForType.Add(memberName, jsonName);
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
JsonProperty prop = base.CreateProperty(member, memberSerialization);
if (_propertyMappingsByType.TryGetValue(member.DeclaringType, out Dictionary<string, string> mappingsForType) &&
mappingsForType.TryGetValue(member.Name, out string jsonName))
prop.PropertyName = jsonName;