I am attempting to code a nested JSON object for an upload API endpoint. I am having issues forming the code for the nested object. When I call the endpoint to post the data to their site, I get a 400 Bad Request error. I am using Newtonsoft.Json. When I remove the DisciplineData from the string, it POSTs successfully, but data in the DisciplineData class is required.
public class UploadClassParameters
{
public int CompYear { get; set; }
public int CompID { get; set; }
public string ClassNumber { get; set; }
public List<Result> Results { get; set; }
}
public class Result
{
public string Placing { get; set; }
public int HorseID { get; set; }
public Dressage DisciplineData { get; set; }
}
public class Dressage // THIS IS THE NESTED OBJECT
{
public double TotalScore { get; set; }
public double TotalPercent { get; set; }
public string DressageLevel { get; set; }
}
Using the code shown here, the classes above create this (which fails when I call the endpoint):
{
"CompYear": 2025,
"CompID": 1214,
"ClassNumber": "53",
"Results": [
{
"Placing": "1",
"HorseID": 5536384,
"DisciplineData": {
"TotalScore": 169.0,
"TotalPercent": 67.6,
"DressageLevel": "FIRST LEVEL, TEST 2",
}
}
]
}
To see how their data is formed, I called their GetClass endpoint, and this was returned:
{
"CompYear": 2025,
"CompID": 1214,
"ClassNumber": "53",
"Results": [
{
"Placing": "1",
"ID": 5536384,
"DisciplineData":{\"TotalScore\":169.0,\"TotalPercentage\":67.6,\"DressageLevel\":\"FIRST LEVEL, TEST 2\";}]}
}
The DisciplineData is the nested object. I am not familiar with the backslashes. And have done some research to see that it is "serializing".
This is the current code:
var elements = new UploadClassParameters
{
CompYear = 2025,
CompID = 1234,
ClassNumber = "12",
Results = new List<Result>
{
new Result
{
Placing = "1",
HorseID = 4567,
DisciplineData = new Dressage
{
TotalScore = 169.0,
TotalPercent = 67.600,
DressageLevel = "FIRST LEVEL, TEST 2",
}
}
},
};
string jsonPayload = Newtonsoft.Json.JsonConvert.SerializeObject(elements);
string signature = CreateSignature(clientID, clientSecret);
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://api.XXXXXX.org"); // API endpoint
// client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", signature);
var content = new StringContent(jsonPayload, System.Text.Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync("/api/results/uploadclass", content);
}