0

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?

Devin Clark
  • 1,078
  • 11
  • 9
  • Will you ever have more than 2 people? For example person1, person2, person3, personN, etc. If so, you should use a collection of people. – Tarzan Jul 26 '17 at 16:06
  • Nope, just the two. – Devin Clark Jul 26 '17 at 16:07
  • You can simply not return person2 if it's null. Then the receiver of the JSON string can test for the existence of person2 and proceed appropriately. Alternatively, you can instantiate each object, populate the values, and then serialize them into a JSON string. You could use empty strings and 0s when person2 doesn't exist. However, that's a bit of a kludge. – Tarzan Jul 26 '17 at 16:14
  • You can use JSON.NET's [`JsonWriter`](http://www.newtonsoft.com/json/help/html/ReadingWritingJSON.htm) to construct arbitrary JSON. Then you wouldn't need to even (de)serialize, but rather just deal with JSON. – Heretic Monkey Jul 26 '17 at 16:24
  • This what you're looking for? [Json Convert empty string instead of null](https://stackoverflow.com/a/35620248/3744182). – dbc Jul 26 '17 at 19:00
  • *someone sends me things like person1.lastname, person2.last4Ssn* - can you share examples of the JSON you are receiving? I believe you only show what you need to return. – dbc Jul 26 '17 at 20:37

2 Answers2

0

If you are using the NewtonSoft Json.Net library to serialize the C# class into JSON then you can decorate the members of the class with [DefaultValue("")]. When you serialize the object it should populate all of the fields with an empty string if they don't have a value, for example:

public class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    [DefaultValue("")]
    public string FullName
    {
        get { return FirstName + " " + LastName; }
    }
}

Taken from the Json.Net documentation here.

Chillidan
  • 1
  • 2
  • This worked but I also had to instantiate each class in the generated class posted above. After doing that and tagging the default value, I was able to run it through the converter and got the same output I was expected. – Devin Clark Jul 27 '17 at 16:08
  • Excellent, glad you got it sorted. – Chillidan Jul 27 '17 at 22:29
0

I figured it out.

For anyone wondering, remove the outer surrounding class, it's not necessary. Then instantiate each class and nested class:

Model model = new Model();
model.summary = new Summary();
model.summary.audit = new Audit();
model.summary.customer = new Customer()
model.summary.customer.Person1 = new Person1();

So on and so on...

Then assign the values which you received in the POST request

model.summary.customer.person1.lastName = request.lastName
model.summary.customer.person2.last4SSN = request.secondaryLast4SSN

Finally, create a JSON string with the result you want:

using Newtonsoft.Json;
...    
return JsonConvert.SerializeObject(model, Formatting.Indented);
Devin Clark
  • 1,078
  • 11
  • 9