I think what I would do in this situation is deserialize the JSON to a list of strongly typed objects, do the manipulation on that list, and then serialize it back to JSON. Json.Net makes the serialization/deserialization parts very easy, so all you need worry about is the query in between.
First, define a simple class for the items:
class Item
{
public string ID { get; set; }
public DateTime EffectiveDate { get; set; }
public string Channel { get; set; }
public string Group { get; set; }
public string Rate { get; set; }
public object MinCharge { get; set; }
public object MaxCharge { get; set; }
}
Then, you can get the result you want using the following code, where json
contains the original JSON from your question:
// A date converter is necessary to handle the non-default date format
IsoDateTimeConverter dateConverter = new IsoDateTimeConverter
{
DateTimeFormat = "dd/MM/yyyy"
};
// deserialize the JSON into a list of Item objects
List<Item> items =
JsonConvert.DeserializeObject<List<Item>>(json, dateConverter);
// find the max date among the items that is strictly less than today's date
DateTime maxDate =
items.Where(i => i.EffectiveDate < DateTime.Today).Max(i => i.EffectiveDate);
// with this max date in hand, filter the list to items matching that date
List<Item> result =
items.Where(i => i.EffectiveDate == maxDate).ToList();
// serialize the resulting list back to JSON
string jsonResult =
JsonConvert.SerializeObject(result, Formatting.Indented, dateConverter);
// print out the new JSON (or whatever you need to do with it)
Console.WriteLine(jsonResult);