using System.Collections.Generic;
using Newtonsoft.Json.Linq;
public static void Main()
""ugly"": ""total garbage""
HandleInfoReceipt(json, out BasicInfoDatabase infoDb);
foreach (var kvp in infoDb)
Console.WriteLine(kvp.Key);
Console.WriteLine(" Id:" + kvp.Value.Id);
Console.WriteLine(" Name:" + kvp.Value.Name);
Console.WriteLine(" Amount:" + kvp.Value.Amount);
private static bool HandleInfoReceipt(string jsonStr, out BasicInfoDatabase infoDb)
infoDb = new BasicInfoDatabase();
JObject respJson = JObject.Parse(jsonStr);
respJson.FilterOutPropertiesThatAreNotCompatibleWith<BasicInfo>();
infoDb = respJson.ToObject<BasicInfoDatabase>();
throw new NullReferenceException("No info found");
public class BasicInfoDatabase : Dictionary<string, BasicInfo>
public int Id { get; set; }
public string Name { get; set; }
public decimal Amount { get; set; }
public static class JsonExtensions
public static void FilterOutPropertiesThatAreNotCompatibleWith<T>(this JObject jo)
var knownItemPropertyNames =
typeof(T).GetProperties()
.Where(p => p.Value.Type != JTokenType.Object ||
!p.Value.Children<JProperty>()
.Any(cp => knownItemPropertyNames.Contains(cp.Name)))
foreach (JProperty prop in unwantedProperties)