I'm currently facing an issue with a fetch request that communicates with an API and receives a JSON object in return. The problem arises when the API includes escape characters for single quotes, such as isn\'t
. This causes an error during the JSON parsing process, leading me to seek a way to manipulate the response body before calling response.json(). Below is the snippet of my existing code:
httpCallout(){
fetch('/1.0/?method=getQuote&lang=en&format=json', {method: "GET", cache: "no-cache"})
.then(response => {
return response.json()
})
.then((data) => {
const quote = data.quoteText;
this.setState({
quote: quote,
author: data.quoteAuthor
});
});
this.colorChange(rgb);
}
The main issue occurs specifically when response.json
encounters an escaped single quote. While I could simply catch the error and move on, I am determined to find a solution that allows me to remove the escaped single quote and still update my state accordingly.
In essence, my goal is to alter a response containing a quoteText value like
"It\'s tiring isn\'t it?"
to be properly parsed as "It's tiring isn't it?"
before executing response.json() to prevent any Bad escape character
errors.