Query a JSON string to fetch all the rows corresponding to maximum value of Date field which is less than or equal to the Current date

StackOverflow https://stackoverflow.com/questions/23265415

  •  08-07-2023
  •  | 
  •  

Question

I have a requirement to query a JSON string to fetch all the rows corresponding to maximum value of Date field which is less than or equal to the Current date

My JSON string is as below,

[{"ID":"1","EffectiveDate":"27/04/2014","Channel":"Buy","Group":"UK","Rate":"7.0700","MinCharge":null,"MaxCharge":null},{"ID":"1","EffectiveDate":"28/04/2014","Channel":"Buy","Group":"UK","Rate":"6.8194","MinCharge":null,"MaxCharge":null},{"ID":"1","EffectiveDate":"27/04/2014","Channel":"Buy","Group":"UK","Rate":"15.3539","MinCharge":null,"MaxCharge":null},{"ID":"1","EffectiveDate":"23/04/2014","Channel":"Buy","Group":"UK","Rate":"2.1100","MinCharge":null,"MaxCharge":null},{"ID":"1","EffectiveDate":"22/04/2014","Channel":"Buy","Group":"UK","Rate":"2.0385","MinCharge":null,"MaxCharge":null},{"ID":"1","EffectiveDate":"23/04/2014","Channel":"Buy","Group":"UK","Rate":"2.1100","MinCharge":null,"MaxCharge":null},{"ID":"1","EffectiveDate":"27/04/2014","Channel":"Buy","Group":"UK","Rate":"2.0357","MinCharge":null,"MaxCharge":null},{"ID":"1","EffectiveDate":"29/04/2014","Channel":"Buy","Group":"UK","Rate":"3.8600","MinCharge":null,"MaxCharge":null},{"ID":"1","EffectiveDate":"18/04/2014","Channel":"Buy","Group":"UK","Rate":"3.7213","MinCharge":null,"MaxCharge":null},{"ID":"1","EffectiveDate":"27/04/2014","Channel":"Buy","Group":"UK","Rate":"151.0686","MinCharge":null,"MaxCharge":null},{"ID":"1","EffectiveDate":"26/04/2014","Channel":"Buy","Group":"UK","Rate":"2.7700","MinCharge":null,"MaxCharge":null},{"ID":"1","EffectiveDate":"27/04/2014","Channel":"Buy","Group":"UK","Rate":"2.6717","MinCharge":null,"MaxCharge":null},{"ID":"1","EffectiveDate":"27/04/2014","Channel":"Buy","Group":"UK","Rate":"0.7500","MinCharge":null,"MaxCharge":null},{"ID":"1","EffectiveDate":"26/04/2014","Channel":"Buy","Group":"UK","Rate":"0.7164","MinCharge":null,"MaxCharge":null},{"ID":"1","EffectiveDate":"27/04/2014","Channel":"Buy","Group":"UK","Rate":"1.9400","MinCharge":null,"MaxCharge":null},{"ID":"1","EffectiveDate":"23/04/2014","Channel":"Buy","Group":"UK","Rate":"2.5500","MinCharge":null,"MaxCharge":null}]

Then the JSON array needs to be queried for the rows that has "maximum EffectiveDate value" and "EffectiveDate value less than or equal to the current date"

i.e., Say if CurrentDate is 24/04/2014 then output should be,

[{"ID":"1","EffectiveDate":"23/04/2014","Channel":"Buy","Group":"UK","Rate":"2.1100","MinCharge":null,"MaxCharge":null},{"ID":"1","EffectiveDate":"23/04/2014","Channel":"Buy","Group":"UK","Rate":"2.1100","MinCharge":null,"MaxCharge":null},{"ID":"1","EffectiveDate":"23/04/2014","Channel":"Buy","Group":"UK","Rate":"2.5500","MinCharge":null,"MaxCharge":null}]

I am using Newtonsoft.Json for writing LINQ queries.

Looking for some help here as I am new to JSON

Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top