Sorting JSON array by date

Please help me to sort the Json array based the date. Please see below input data. The out put should come the latest date item should be on top. I have tried this one but its throwing the exception

eventsJsarray.Cast(Of JObject).Where(Function (x) x(“event”).toString).OrderByDescending(function(x) Cdate(x(“loggedDate”).ToString)).FirstOrDefault

"event": [
        {
            "agnNumber": "123456",
			"agnName": "Mathew",
			"City": "Chennai",
			"loggedDate": "2025-01-26 09:45:49.926",
           
        },
		   {
            "agnNumber": "123768",
			"agnName": "enraled",
			"City": "hyd",
			"loggedDate": "2025-01-25 09:45:49.926",
           
        },
		   {
            "agnNumber": "123458",
			"agnName": "Mathew",
			"City": "bnglr",
			"loggedDate": "2024-01-24 09:45:49.926",
           
        },
		   {
            "agnNumber": "123458",
			"agnName": "Mathew",
			"City": "mubai",
			"loggedDate": "2025-01-27 09:45:49.926",
           
        }
]

@thotlamahesh_kumar ,

Not tested but can you try this one

' Assume you have a JSON string stored in a variable named jsonString
Dim jsonArray As JArray = JArray.Parse(jsonString)
Dim sortedArray As JArray = JArray.FromObject(jsonArray.OrderByDescending(Function(x) DateTime.Parse(x("loggedDate").ToString())).ToList())

@thotlamahesh_kumar

this is how you would sort

jarr_events.OrderBy(function(x) DateTime.ParseExact(x("loggedDate").ToString,"yyyy-MM-dd HH:mm:ss.fff",System.Globalization.CultureInfo.InvariantCulture)).FirstOrDefault

input json

cheers

Thanks @ashokkarale for your reply
let me try and i will provide the update

1 Like

Thanks a lot @Anil_G , its worked but results came in reverse order
the old date came first and new date came last

1 Like

@thotlamahesh_kumar

Then use .Last

Or use OrderByDescending instead of OrderBy

Hope this helps

Cheers

@Anil_G
The Values getting in the JToken format but subsequent steps we are using Jobject
could you please help me how to convert to Jobject from JToken or do we have way to sort the process on Jobjet level?

@thotlamahesh_kumar

USE .ToJObject

Cheers

@Anil_G
I am getting below error while converting JToken to JObject

@thotlamahesh_kumar

first thing it is ToJObject you gave ToObject

cheers

The main part would be
grafik

we can see, that we got an ordered Enumerable of JObject.

So now it depends on what you need

A list/Array of JObjects:
grafik

A JArray:

or let us know what else is needed

Screnshots done within immediate panel
Understanding the 6 Debugging Panels of UiPath in the easiest way possible! - News / Tutorials - UiPath Community Forum

UPD1 - reference to used Sample JSON involved within a Deserialize JSON:

@Anil_G
I dont see any ToJobject
The Values in Jtokents so we need to convert to Jobject

@thotlamahesh_kumar

please try this

jarr_events.OrderBy(function(x) DateTime.ParseExact(x("loggedDate").ToString,"yyyy-MM-dd HH:mm:ss.fff",System.Globalization.CultureInfo.InvariantCulture)).FirstOrDefault().ToObject(Of JObject)

cheers

Thanks @ppr

same expression i have used but still getting some error
The HistoryEvent is argument and its type Jobject

eventJsobj(“event”).Values(Of JObject).OrderByDescending(Function (x) x(“loggedDate”).Value(Of DateTime))

with the usage of the immediate panel. we do have a good verification on its success. It was working as we refer to the screenshots

Related to the error it looks like you are trying to assing the result to a JObject.
As showcased above related to the different options keep in mind:

is similiar to
The main part would be
grafik
and therefore not a JObject

So adjust the DataType of HistoryEvent VCariable to List(of JObject)
and modify th stetement by appending toList

Assign Activity:
HistoryEvent =

eventJsobj("event").Values(Of JObject).OrderByDescending(Function (x) x("loggedDate").Value(Of DateTime)).toList

which is similar to:

And also keep in mind our statement:

json_sorting.xaml (10.3 KB)

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.