0

Don't know if it is possible but I want to have a different object name in json and in class when I'm calling REST API, e.g. my Json would look like:

{
  "JsonName": {
    "Prop1": "value1",
    "Prop2": "value2",
    "Prop3": "value3"
  },
  "Example": "value4"
}

And I would call a method which would look like:

public async Task<ActionResult<SomeResponse>> PostCodeName(
    [FromBody] Wrapper wrapper)
    {
        // All the code
    }   

public class Wrapper
{
    public SomeClass CodeName{get; set;}
    public int Id {get; set;}
} 

What I'm asking is basically if it is possible to use JsonName instead of CodeName in json object when calling API (maybe some kind of an attribute? I haven't found anything unfortunately).

Also can I do the same for the Wrapper(different class name but same properties) when consuming API from different app?

  • 2
    Yes - you can add an attribute, but which attribute you need to add will depend on which serializer you're using. Use `[JsonProperty("JsonName")]` in Newtonsoft.Json, or `[JsonPropertyName("JsonName")]` in System.Text.Json. – Jon Skeet Jan 27 '22 at 18:52
  • Works well, thank you – Filip Tobiasz Jan 27 '22 at 19:04

0 Answers0