Home > Back-end >  Deserializing JSON with this curly braces embedded in a string
Deserializing JSON with this curly braces embedded in a string

Time:06-22

One of the nodes in this JSON payload is driving me nuts.

It's formatted like a string (inside quotes), but it will not go into a string on my object.

Here is the entire payload just as I receive it:

{
    "Event_Id":"da89afe72b41cb685f03261d8cb18d9e",
    "Event_Name":"Employee Created",
    "Event_DateTime":1655144130,
    "ClientCode":"XXXXX",
    "Resource_Field":"Employee",
    "Resource_Identifier":null,
    "Object":null,
    "Object_Identifier":"9995",

    "Data":"{"Employee_ID":"9995","Is_Rehire":"N","Was_New_Hire":"Y"}",

    "Endpoint":"Employee",
    "EndpointUrl":"api/v1/employee/9995"
}

The problem is the "Data" property, separated it for clarity.

And here is the object that it loads to:

public class OutsideEvent
{
    public string Event_Id { get; set; }
    public string Event_Name { get; set; }
    public int Event_DateTime { get; set; }
    public string ClientCode { get; set; }
    public string Resource_Field { get; set; }
    public object Resource_Identifier { get; set; }
    public object Object { get; set; }
    public string? Object_Identifier { get; set; }

    **public string? Data { get; set; }**

    public string Endpoint { get; set; }
    public string EndpointUrl { get; set; }
}

Using Postman, I can remove the quotes from around the "Data" value, change the type to object and deserialize it into this:

public class eventData
{
    public string Employee_ID { get; set; }
    public string Is_Rehire { get; set; }
    public string Was_New_Hire { get; set; }
}

But I can't make the sender remove the quotes, so I have to deal with it somehow?

Also tried decorating the Data property with [JsonIgnore] since I don't care about this value, but it didn't change anything.

Any help would be greatly appreciated.

Thanks!

CodePudding user response:

json data property is an invalid string, since it contains double quotes included in double quotes

....
"Data":"{"Employee_ID":"9995","Is_Rehire":"N","Was_New_Hire":"Y"}",
....

so just remove outer double quotes, you can do it using string replace function

    json = json.Replace("\"{", "{").Replace("}\"","}");

now you have a valid json

...
"Data":{"Employee_ID":"9995","Is_Rehire":"N","Was_New_Hire":"Y"},
....

and you can deserialize it to c#

    Root root=JsonConvert.DeserializeObject<Root>(json);

Classes

public class Data
{
    public string Employee_ID { get; set; }
    public string Is_Rehire { get; set; }
    public string Was_New_Hire { get; set; }
}

public class Root
{
    public string Event_Id { get; set; }
    public string Event_Name { get; set; }
    public int Event_DateTime { get; set; }
    public string ClientCode { get; set; }
    public string Resource_Field { get; set; }
    public object Resource_Identifier { get; set; }
    public object Object { get; set; }
    public string Object_Identifier { get; set; }
    public Data Data { get; set; }
    public string Endpoint { get; set; }
    public string EndpointUrl { get; set; }
}
  • Related