How do I convert a JArray to a regular Array?

I’m trying to do a sort on dictionary keys. This works fine:

(From kvp In TransactionItem.SpecificContent Order By Array.IndexOf({"Result Details","Result","Date/Time Processed"}, kvp.Key) Descending).ToDictionary(Function (x) x.Key,Function (x) x.Value)

So I decided to move the array to the config file with a JSON format value:

["Result Details","Result","Date/Time Processed"]

I can parse it and get this output:

JArray.Parse(Config("Transaction Key Order").ToString)
image

But for some reason I can’t .ToArray it:

It actually accepts the .ToArray in the sort expression…
(From kvp In TransactionItem.SpecificContent Order By Array.IndexOf(JArray.Parse(Config("Transaction Key Order").ToString).ToArray, kvp.Key) Descending).ToDictionary(Function (x) x.Key,Function (x) x.Value)

That gives no errors but when I run the automation it does not sort the keys.

Is there some trick to converting a JArray into a regular array?

Tried this way, still no luck.

(From kvp In TransactionItem.SpecificContent Order By Array.IndexOf(JsonConvert.DeserializeObject(Of String())(Config("Transaction Key Order").ToString).ToArray, kvp.Key) Descending).ToDictionary(Function (x) x.Key,Function (x) x.Value)

grafik

1 Like

Tried this, still doesn’t work. It doesn’t throw any errors, but the order of the keys doesn’t actually change.

(From kvp In TransactionItem.SpecificContent Order By Array.IndexOf(JArray.Parse(Config("Transaction Key Order").ToString).ToObject(Of String()), kvp.Key) Descending).ToDictionary(Function (x) x.Key,Function (x) x.Value)

Finally! This one worked:

(From kvp In TransactionItem.SpecificContent Order By Array.IndexOf(JArray.Parse(Config("Transaction Key Order").ToString).Values(Of String).ToArray, kvp.Key) Descending).ToDictionary(Function (x) x.Key,Function (x) x.Value)

Although I have no idea why only that one works. Seems like all the others should work too. I suspect the other methods were giving an array of JObject or JToken or something, instead of array of string.

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