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
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...
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:
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.
if all your json strings are have only 2 properties Id and Description, you can try to fix your json string using this code