skip to Main Content

How do I deserialize JSON that has unescaped double quotes in C#?

JSON string example:

{
 "Id": "4c8e6419-f094-48a0-94f8-752ccd7f0354",
 "Description": "Anastasia Sfeir is a Mexican actress, singer, and model. She is best known for her roles in the telenovelas "La que no podia amar" and "Lo que la vida me robo"."
}

the JSON string exactly looks like this:

string json = "n{n "Id": "78d4f53a-8614-4096-ace9-72a62dbcdbc3",n "Description": "100 Thieves is an esports organization and lifestyle brand founded in 2016 by Matthew "Nadeshot" Haag."n}n";

I’m getting exceptions, but once I removed the quotes, it worked.

result = System.Text.Json.JsonSerializer.Deserialize<Description>(response);

I have no control over the response, so I hope there is an easy built-in way to handle this.

4

Answers


  1. Chosen as BEST ANSWER

    Inspired by @Serge answer, I devised this method to clean the description. Since this JSON is a reply from an AI chatbot, so sometimes it's not 100% correct, which will cause a loss of money for the tokens used...

         static string FixAndDeserializeJson(string json)
            {
                try
                {
                    // Extract the 'Description' value using a regular expression.
                    var descriptionPattern = @"""Description"":s*""(.*?)""";
                    var match = Regex.Match(json, descriptionPattern);
    
                    if (match.Success)
                    {
                        // Get the matched group which contains the description
                        var descriptionValue = match.Groups[1].Value;
    
                        // Decode Unicode characters and remove backslashes and quotes
                        var fixedDescription = DecodeEncodedNonAsciiCharacters(descriptionValue)
                            .Replace("\", "").Replace(""", "");
    
                        // Replace the old description with the fixed one in the original JSON string
                        var fixedJson = Regex.Replace(json, descriptionPattern, $""Description": "{fixedDescription}"");
    
                        // Validate the fixed JSON by attempting to parse it.
                        JToken.Parse(fixedJson);
    
                        // If parsing is successful, return the fixed JSON
                        return fixedJson;
                    }
                    else
                    {
                        // If the description isn't found, the JSON is likely invalid or not in the expected format
                        return null;
                    }
                }
                catch (Exception)
                {
                    // If parsing throws an exception at any point, the JSON is invalid; return null
                    return null;
                }
            }
    
    
            static string DecodeEncodedNonAsciiCharacters(string value)
            {
                return Regex.Replace(
                    value,
                    @"\u(?<Value>[a-fA-F0-9]{4})",
                    m => {
                        return ((char)int.Parse(m.Groups["Value"].Value, System.Globalization.NumberStyles.HexNumber)).ToString();
                    });
            }
    
    

  2. The example JSON payload you provided doesn’t have double quotes. Quotes inside of a JSON string are escaped with a backslash (eg: "). If your string example is exactly how the website is returning it, then that is not valid JSON and cannot be read by a regular JSON parsing library.

    If the example string had escaped quotes, it would work fine without any modifications. The issue is with the string you’re trying to parse.|

    Example of valid JSON with escaped quotation marks:

    {
     "Id": "4c8e6419-f094-48a0-94f8-752ccd7f0354",
     "Description": "Anastasia Sfeir is a Mexican actress, singer, and model. She is best known for her roles in the telenovelas "La que no podia amar" and "Lo que la vida me robo"."
    }
    
    Login or Signup to reply.
  3. Use postman to call the API manually first, and see if the response is valid.

    Another issue is the JSON string exactly looks like this:. How did you see the json string? Through debugger?

    I'm getting exceptions – what exceptions? Provide debug detail.

    How did you get the HttpResponse? And how did you read it into a string? Provide codes, so we can help you.

    Login or Signup to reply.
  4. if all your json strings are have only 2 properties Id and Description, you can try to fix your json string using this code

    var desc = ""Description":";
    var firstIndex = json.IndexOf(desc) + desc.Length + 1;
    var lastIndex=json.LastIndexOf(""") + 1;
            
    json = json.Replace( json[firstIndex..lastIndex],JsonConvert.SerializeObject(json[(firstIndex+1)..(lastIndex-1)]));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search