We have an api which fetches the translation (from google translate api) and sends a json with some additional data. Problem is that JSON.parse throws error if there is any html element in it, even though the json is perfectly valid.
example:
"{"AutoTranslateIsSuccess" : "true","OriginalString" : "<meta charset="utf-8">Hand Wash Cold","TranslatedString" : "<meta charset="utf-8">Käsinpesu kylmänä","SourceLanguageCode" : "en","TargetLanguageCode" : "fi"}"
Error info : Uncaught SyntaxError: JSON.parse: expected ',' or '}' after property value in object at line 1 column 184 of the JSON data
Why am I sure that JSON is valid? 2 reasons:
- from jsonlint
- because this seems to happens only when there is html. eg the below json does not have any problem
"{"AutoTranslateIsSuccess" : "true","OriginalString" : "Asymmetrical Fit","TranslatedString" : "Epäsymmetrinen istuvuus","SourceLanguageCode" : "en","TargetLanguageCode" : "fi"}"
so if this was a problem with code or the returned JSON the second case should also throw error, right?
Code where the problem happens:
LTjqs.get(url,
requestData,
function (data, textStatus, jqXHR) {
var responseJson = JSON.parse(data);//error happens here
Note to mods: this is not a duplicate. I have gone thru the other JSON.parse answers but those are slightly different from my case.