-1

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:

  1. from jsonlint
    json lint
  2. 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.

freedomn-m
  • 24,983
  • 7
  • 32
  • 55
maX
  • 764
  • 2
  • 11
  • 31
  • 4
    You must escape `"` in the JSON string if they're apart of the value – evolutionxbox May 25 '22 at 14:48
  • `JSON.parse()` is not broken. If it's giving you an error, there's an error in the value your API returns. It has nothing to do with whether there's HTML markup in the serialized objects. – Pointy May 25 '22 at 14:51
  • 1
    "*Note to mods: this is not a duplicate.*" - might not be a "duplicate", but [this answer](https://stackoverflow.com/a/15637481/2181514) provides the solution to your problem – freedomn-m May 25 '22 at 14:52
  • 1
    "*even though the json is perfectly valid*" - no, no it's not. – freedomn-m May 25 '22 at 14:52
  • @freedomn-m Can you explain? jsonlint think its valid – maX May 25 '22 at 14:54
  • See comments above – freedomn-m May 25 '22 at 14:54
  • @Pointy Then why no error in the second JSON? – maX May 25 '22 at 14:54
  • Because you don't have HTML in the second JSON. The first is different to the second – evolutionxbox May 25 '22 at 14:58
  • 2
    *this seems to happens only when there is html* -> this only happens when there are `"` in the values – freedomn-m May 25 '22 at 15:00
  • I've rolled back the edit as it removed/added various `"` and `\"` - please provide a code snippet showing the json that's used so that it can be repeated - it's hard to tell if the `"` and `\"` have been added so that the code *shows* correctly in the SO question or if that's your actual source (eg is it with `Hand Wash Expecting 'EOF', '}', ':', ',', ']', got 'undefined'` (which is essentially the same as JSON.parse error) – freedomn-m May 25 '22 at 15:10
  • You seem to mix up a string literal and the actual string value. See [this Fiddle](https://jsfiddle.net/fz4thgns/) and enter the JSON shown in the result window (after "Trying to parse JSON:") into JSONLint. You'll see that it isn't valid. – Ivar May 25 '22 at 15:13
  • @freedomn-m I didn't add those. They were already part of the string, but not formatted properly so not shown in the rendered post. See the Markdown in the revision history. – Ivar May 25 '22 at 15:14
  • Exactly - can you make it a snippet so that it shows as-is. The "markdown" in the question is being rendered and that's what everyone is used - *as seen in the question* - we're not expected to extract that from the revision history/question source. – freedomn-m May 25 '22 at 15:17
  • You need to look at the *actual response content* via the browser Network developer tab. Nobody can tell what your server is doing to construct the broken JSON response text, and that's probably where the problem lies. – Pointy May 25 '22 at 15:21
  • @maX String you posted is not valid, pls post the real one. – Serge May 25 '22 at 19:43

0 Answers0