Rushikesh Joshi
Rushikesh Joshi

Reputation: 137

C# WebAPI parse Json data with extra braces

This may be a very rare scenario, but I ended up with a problem and not finding any solution.

I have a WebAPI controller written with MVC WebAPI in C#. I have a post method which receives the custom object.

[HttpPost]
public HttpResponseMessage SendEmail([FromBody]Email email)
{

}

Below is the Email class, note that EmailData is of dynamic type.

public class Email
{
    public string EmailTo { get; set; }

    public dynamic EmailData { get; set; }
}

When I am sending JSON payloads as the post, EmailData property is adding extra curly braces '{}' into the object. This is giving me a problem because my dynamic data is not able to parse JSON with extra curly braces '{}'.

{
  "EmailFrom": "[email protected]",
  "EmailData": {
    "FirstName": "Rushi",
    "LastName": "Joshi"}
}

If this is happening because of the dynamic type, then I don't want to change type as I am not sure what all Key/Value requester may send. I am using dynamic data to extract data from it.

This shows EmailData being parsed and adding extra curly braces.

enter image description here

Upvotes: 3

Views: 2037

Answers (4)

Rushikesh Joshi
Rushikesh Joshi

Reputation: 137

Thanks everyone, yes you all gave a wonderful answer and special thanks to Stevenfowler16, who describe that this is correct way JSON object should be processed.

Thing is I can't remove the dynamic object type as that is coming from some other library, also I can't change anything into the signature of API. but yes below thing helped me.

em.EmailData = System.Web.Helpers.Json.Decode(em.EmailData.ToString());

This serves my purpose but again thanks, everyone, your solution provide me some direction into it.

Regards

Rushi

Upvotes: 0

Lav Vishwakarma
Lav Vishwakarma

Reputation: 1426

If you want to store the dynamic JSON in your Email class then you can use the Newtonsoft.Json package.

    public class Email
    {
        public string EmailTo { get; set; }

        public JOject EmailData { get; set; }
    }

It will store any type of the JSON. So you can parse the inner JSON object at runtime.

Upvotes: 0

programtreasures
programtreasures

Reputation: 4298

Use Dictionary object instead of dynamic, it will bind your key value pair data,

    public class Email
    {
        public string EmailTo { get; set; }

        public Dictionary<string,string> EmailData { get; set; }
    }

Your json will be,

{
  "EmailTo": "[email protected]",
  "EmailData": {
    "FirstName": "Rushi",
    "LastName": "Joshi"}
}

Upvotes: 3

Stevenfowler16
Stevenfowler16

Reputation: 1000

The JSON provided is valid: https://jsonlint.com/

An object with more than one property will always return in that format.

"EmailData": {
    "FirstName": "Rushi",
    "LastName": "Joshi"}

This is an EmailData object with two name/value pairs.You have an object nested inside your original object.

Here is some documentation to read up on for further clarification. https://www.json.org/

Upvotes: 0

Related Questions