When I retrieve data from an API, I receive a STRING like this:
[
{
"reason": "Invalid address",
"email": "j\u00c3\u00a9r\u00c3\u00b4mel\u00c3\u00a4ufer@com"
},
{
"reason": "Invalid address",
"email": "dsfdsf"
},
]
After parsing the JSON data with JSON.parse(data) and returning it, I noticed strange characters in the frontend rendering of the page like:
email: "jérômeläufer@com"
I want to either escape the \u00c3\u00a9r... characters or encode them, as I need to use these email addresses as parameters for certain actions. However, the weird characters are preventing me from obtaining the correct email.
Is there a way to achieve this?
solution
To address this issue, I am fetching the list of invalid user emails from SendGrid's API using:
GET https://api.sendgrid.com/api/invalidemails.get.json?api_user=user&api_key=key
The returned data looks like this:
[
{
"reason": "Invalid address",
"email": "j\u00c3\u00a9r\u00c3\u00b4mel\u00c3\u00a4ufer@com"
},
{
"reason": "Invalid address",
"email": "dsfdsf"
},
]
To display the string "j\u00c3\u00a9r\u00c3\u00b4mel\u00c3\u00a4ufer@com" correctly on the web page, I used:
decodeURIComponent(escape(string))
When I needed to remove this email and call the remove email API from SendGrid using:
POST(which is weird,but it's official delete method) https://api.sendgrid.com/api/invalidemails.delete.json
The body of the request is:
body:{
user: ..,
key: ...,
email:unescape(encodeURIComponent(email))
}
This approach worked successfully. Does anyone know why?