How to Format the nested string json property serialized json in C#

Viewed 52

I am facing problem to serialized or format nested string JSON. Here is the input: Data can have dynamic values

{
  "Id": 33,
  "Data": "{\n    \"$Datatype\": \"Val1, Val2\"\n }",
  "Name": "Test"
}

I want the output without any special characters like \n, , \ etc like:

{
    "Id": 33,
    "Data": {
        "$Datatype": "Val1, Val2"
    },
    "Name": "Test"
}
1 Answers

Data property was serialized twice. To fix it try this code

var jsonParsed = JObject.Parse(json);

jsonParsed["Data"] = JObject.Parse((string) jsonParsed["Data"]);

json = jsonParsed.ToString();
Related