Scythe
Scythe
CC#
Created by Scythe on 1/5/2023 in #help
❔ Sorting a JObject by child values
I have JSON being returned by an API that I'm trying to sort to find the most relevant result. Here's an example of the JSON
{
"data": {
"total": 2,
"tracks": [
{
"id": 170778,
"type": "0",
"title": "Wicked Wonders",
"playCount": 0,
"rating": 0,
"tags": []
},
{
"id": 210755,
"type": "0",
"title": "The Wonders Of The Universe",
"playCount": 1,
"rating": 3,
"tags": []
}
]
}
}
{
"data": {
"total": 2,
"tracks": [
{
"id": 170778,
"type": "0",
"title": "Wicked Wonders",
"playCount": 0,
"rating": 0,
"tags": []
},
{
"id": 210755,
"type": "0",
"title": "The Wonders Of The Universe",
"playCount": 1,
"rating": 3,
"tags": []
}
]
}
}
I need to work out which track is the most relevant which I was going to by comparing/sorting by playCount/rating values (So Wonders of the universe would be the preferred result above) but I'm struggling to find a good way to sort the track array by the child values. Can someone point me in the right direction?
18 replies
CC#
Created by Scythe on 1/4/2023 in #help
JSON child objects within a RestRequest body
I have a working API call in Postman that gets data from another application using a filter. Using the C# code that Postman outputs works, but doesn't seem to include the filter object within my json body. So with the example below: C# code
var client = new RestClient("http://localhost:XXXX/v1/search");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Content-Type", "application/json");
var body = @"{""source"": ""non-archived"", ""fields"": [""type"", ""title"", ""tags"", ""cuepoints"", ""data""], ""filter"": { ""artist"": ""Dr. Peacock"", ""title"": ""Medication"" } }";
request.AddParameter("application/json", body, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
var client = new RestClient("http://localhost:XXXX/v1/search");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Content-Type", "application/json");
var body = @"{""source"": ""non-archived"", ""fields"": [""type"", ""title"", ""tags"", ""cuepoints"", ""data""], ""filter"": { ""artist"": ""Dr. Peacock"", ""title"": ""Medication"" } }";
request.AddParameter("application/json", body, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
JSON Body in Postman
{
"source": "non-archived",
"fields": ["type", "title", "tags", "cuepoints", "data"],
"filter": { "artist": "Dr. Peacock", "title": "Medication" }
}
{
"source": "non-archived",
"fields": ["type", "title", "tags", "cuepoints", "data"],
"filter": { "artist": "Dr. Peacock", "title": "Medication" }
}
When run in Postman, I correctly get 3 results returned that match the filter arguments. When run in C# I get 18,000 results as the filter object doesn't appear to be passed correctly and it's only getting the source/field values. I'm assuming I need to do something else to wrap the filter object within the json body object so that it's parsed correctly but I haven't been able to work out what that syntax should be
5 replies