I am trying to create a JSON string with custom values received from an API post.
The post indicates that the fields that will go into the JSON string are optional but the JSON string needs to be created regardless, with those fields left empty if we do not receive them.
I tried to take the string and create a C# object out of it but it is filled with nested objects.
How do I fill in information to nested JSON objects when some can be empty? It all must be returned.
Example JSON string (shortened and values changed):
{
"summary": {
"audit": {
"id": "123456",
"number": "11-123456",
"filingTime": "2011-04-28T00:00:00",
"suspended": 0.0
},
"customer": {
"person1": {
"lastName": "JOHN",
"firstName": "SMITH",
"last4SSN": "1234"
},
"person2": {
"lastName": "",
"last4SSN": "0000"
},
"address": "1234 WASHINGTON ST3"
},
"information": {
"id": "13"
},
"results": {
"number": {
"id": "12345678"
},
"disputes": []
}
}
Example C# model created:
public class Model {
public class Audit
{
public string id { get; set; }
public string number { get; set; }
public DateTime filingTime { get; set; }
public double suspended { get; set; }
}
public class Person1
{
public string lastName { get; set; }
public string firstName { get; set; }
public string last4SSN { get; set; }
}
public class Person2
{
public string lastName { get; set; }
public string last4SSN { get; set; }
}
public class Customer
{
public Person1 person1 { get; set; }
public Person2 person2 { get; set; }
public string address { get; set; }
}
public class Information
{
public string id { get; set; }
}
public class Number
{
public string id { get; set; }
}
public class Results
{
public Number number { get; set; }
public IList<object> disputes { get; set; }
}
public class Summary
{
public Audit audit { get; set; }
public Customer customer { get; set; }
public Information information { get; set; }
public Results results { get; set; }
}
public class Model
{
public Summary summary { get; set; }
}
}
So then someone sends me things like person1.lastname, person2.last4Ssn - I would need to send back a JSON string like the example above with everything empty except those two fields.
What is the correct way to approach this? Instantiate each object and enter them by hand and then serialize the object into a JSON string?