I'm using Newtonsoft to deserialize JSON data to an object.
My JSON looks like this:
{
"id": "4aa50d01-41bd-45e3-803e-f479a948acf1",
"referenceNumber": "120064",
"status": "Application in Progress",
"borrowers": [
{
"name": "John Doe",
"type": "BORROWER"
},
{
"name": "Jane Doe",
"type": "COBORROWER"
}
],
"propertyAddress": {
"zipCodePlusFour": ""
}
}
The borrowers array can have up to 2 items. 1 with type == "BORROWER"and the other with type == "COBORROWER"
I have a LoanItem class I am deserializing to.
public class LoanItem
{
public string referenceNumber { get; set; }
public string status { get; set; }
}
I know I can mark the LoanItem property with the JSONProperty attribute but I'm wondering if there is a way I can add an array sub item with a condition.
Something maybe like
[JSONProperty("borrowers[WHERE type = 'BORROWER'].name")]
public string BorrowerName { get; set; }
[JSONProperty("borrowers[WHERE type = 'COBORROWER'].name")]
public string CoBorrowerName { get; set; }
Is this possible? Can I use the JSONProperty attribute?
JsonPropertyAttributeis not designed to do what you want in that way. It's a way to map a simple json property to a .net property. What you're proposing is far more complex than any of the APIs provide. If you want more complex logic, you'll have to be the one to provide it.