0

I have a model class as given below:

 public class ApiRequest{
    [JsonProperty(PropertName="name")]
    public string Name{get;set;}
    [JsonProperty(PropertName="description")]
    public string Description{get;set;}
    [JsonProperty(PropertName="privacy.view")]
    public string PrivacyStats {get;set}
}

And when I call the API, it should convert this model to a json with nested object for privacy status as below

    {
         "name":"Test1",
         "description":"Testing Only",
         "privacy":{
               "view":"unlisted"
}
    }

How can I map single model to nested object with out creating another class for the same?

3
  • Did you try using dynamic type for the status ? Commented Oct 18, 2021 at 7:13
  • Depending on the JSON serializer you are using (ex: Newtonsoft json) you will have to implement a proper converter. You can check this example for newtonsoft: newtonsoft.com/json/help/html/CustomJsonConverter.htm Commented Oct 18, 2021 at 7:22
  • It's pretty easier to add additional model for json field privacy with one field, than implement custom converter and maintain it. Commented Oct 18, 2021 at 8:55

1 Answer 1

1

You should declare one more model for PropertyStats. Otherwise, it will be kind of reverse engineering to modify the already created JSON string.

Like this:

public class ApiRequest
{
    [JsonProperty(PropertyName = "name")]
    public string Name { get; set; }
    [JsonProperty(PropertyName = "description")]
    public string Description { get; set; }
    [JsonProperty(PropertyName = "privacy")]
    public PropertyStats PrivacyStats { get; set; }
}

public class PropertyStats
{
    [JsonProperty(PropertyName = "view")]
    public string View { get; set; }
}

Then You can serialize it like this.

static void Main(string[] args)
{
    ApiRequest objApiRequest = new ApiRequest()
    {
        Name = "Test1",
        Description = "Testing Only",
        PrivacyStats = new PropertyStats
        {
            View = "unlisted"
        }
    };

    var serializedString = JsonConvert.SerializeObject(objApiRequest, Formatting.Indented);

    Console.WriteLine(serializedString);
    Console.ReadLine();
}

Output:

{
  "name": "Test1",
  "description": "Testing Only",
  "privacy": {
    "view": "unlisted"
  }
}

In the future, if you want to add additional property to the privacy model then it will be easy to serialize it.

Like below:

public class PropertyStats
{
    [JsonProperty(PropertyName = "view")]
    public string View { get; set; }
    [JsonProperty(PropertyName = "status")]
    public int StatusCode { get; set; }
}

Serialize like it below:

static void Main(string[] args)
{
    ApiRequest objApiRequest = new ApiRequest()
    {
        Name = "Test1",
        Description = "Testing Only",
        PrivacyStats = new PropertyStats
        {
            View = "unlisted",
            StatusCode = 1
        }
    };

    var serializedString = JsonConvert.SerializeObject(objApiRequest, Formatting.Indented);

    Console.WriteLine(serializedString);
    Console.ReadLine();
}

Output:

{
  "name": "Test1",
  "description": "Testing Only",
  "privacy": {
    "view": "unlisted",
    "status": 1
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.