I am creating a library that takes an in-argument for desired arrival date and time - eg. “03-07-2024” and “08:00”.
This input is forwarded to an API, that returns available trips and tickets.
This API returns about 5/6 available trips around the desired travel time, and now I need to return the “closest available trip” based on the desired arrival.
The API-result could be something like this:
Trip 1: 02-07-2024 21:54
Trip 2: 02-07-2024 22:05
Trip 3: 03-07-2024 04:45
Trip 4: 03-07-2024 07:43
Trip 5: 03-07-2024 10:46
My plan is to extract these results in a List or Datatable, but how do I find the closest one?
Bear in mind that the results can be different dates.
If yes, then store all the data in a List of String datatype variable. Check the below steps,
→ Use the Assign activity to assign the values to a List as below,
Perfect, so we can refer now to the details, and also see that the JSON values are not exact the same as the initial shared input sample. Such details are very important.
JSON deserialization:
myJObject
So we can check how much we can grab by out oft the box conversions (we recommend to confirm, that day, month position are crosschecked, where exact is the day?)
TimeSpan is Failing, but can be handled in that way:
So we can use LINQ and order to the relevant JObject
My problem is not with extracting the data from the API
The data could be in whatefter format you like.
My question is how do I compare a date and time (DateTime) to eg. 5/6 other DateTimes and find the closest match?
The date could be forward or backward in time - I just need the closest match to my input.
(From jo In myJObject("Trip").Values(Of JObject)
Let dp = jo("date").Value(Of DateTime)
Let dt = TimeSpan.Parse(jo("time").toString)
Where dp = myFilterDate.Date
Where dt <= myFilterDate.TimeOfDay
Order By dt
Select jof=jo).Last()
requires adapations (initial requierement was different stated) let us do the following
order all given dates from closest to farest from the input, but filter out any dates later as the given filter date (which is a variable and is therefore dynamizable)
Assign Acitvity:
arrJOFilterResult | Array Of JObject =
(From jo In myJObject("Trip").Values(Of JObject)
Let sd = jo("date").Value(Of String)
Let st = jo("time").Value(Of String)
Let dp = CDate(sd + " " + st)
Where dp <= myFilterDate
Order By dp Descending
Select jof=jo).ToArray
So, trip 3 closet to input but after 8:00
trip 2,1 before input
Using the Ticks and Math.Abs can help to handle such cases. But we would feel that a threshold how far after input date and/or prioritized ordering (what before, what after) can maybe also adress the needs
Let us know if there is interest in such approaches as well
I fail to see how this requirement is different from initially stated.
I literaly wrote:
This API returns about 5/6 available trips around the desired travel time, and now I need to return the “closest available trip” based on the desired arrival.
…
Bear in mind that the results can be different dates.
I would rather that a List or Datatable was used in stead of this overly complicated runthrough of the JSON.
The initial question was not how to read/process the API-results but just how to find the DateTime closest among several other.
My plan is to extract these results in a List or Datatable, but how do I find the closest one?