Search code examples
c#.netasp.net-core

Want to parse few properties as JSON object and other as normal


Is it possible to parse a few properties as JSON object only? Something like this.

public class Request{
    public string Title { get; set; }
    public JObject? Address { get; set; }
    public List<JObject> Addresses { get; set; }
}
Request JSON could look like
{
    "Title":"My title",
    "Address":{
        "property1":1
        "property2":2
    },
    "Addresses":[
    {
        "property1":1
        "property2":2
    },
    {
        "property1":1
        "property2":2
    }
    ]
}

By making the JSON properties as string and sending the stringify JSON we can achieve this. But that is something I'm trying to avoid.


Solution

  • Actually serialization is not a issue. Actually behavior varies in case of controller. What I have observed that after adding [FromBody] it was able to parse it. Even though there's a similar API I created for testing with different request model that do not have JObject. It worked without this attribute [FromBody]. Looks like [FromBody] is required for this case or have some sort of relation.