0

I get JSON data from the Yandex.Direct API, but when I try to parse it there is an error:

SyntaxError: Unterminated string literal (line 231, file "importJSON")

My code:

var jsondata = UrlFetchApp.fetch('https://api.direct.yandex.ru/v4/json/', options);
var contextText = jsondata.getContentText();    
var object   = JSON.parse(contextText);

I think that this problem may be caused by invisible symbols U+2028, U+2029 (http://timelessrepo.com/json-isnt-a-javascript-subset), but I can't find it in the result file.

Please share any suggestions.

UPDATE: I cannot post the resulting JSON here because it contains sensitive production data.

The error appears when using the method
http://api.yandex.ru/direct/doc/reference/GetBanners.xml - 1 request with 10 campaign_ids = [8388422,8396871,8409767,8409910,8409979,8434877,8434885,8434891,8435993,8446636];

If I use this method ten times with one campaign_id in the request, there is no such problem. It's very strange.

faintsignal
  • 1,828
  • 3
  • 22
  • 30
Anton Barycheuski
  • 712
  • 2
  • 9
  • 21
  • You should post the JSON that is returned from the API. It doesn't make a lot of sense that you'd get a JavaScript object back that itself contains a JSON expression; that would be weird. – Pointy Mar 23 '14 at 14:33
  • 1
    what does `getContentText()` do? What's in `contextText`? It sounds like you either are getting bad data or that you are distorting it yourself someway before parsing it. – Karl-Johan Sjögren Mar 23 '14 at 14:33
  • methods from here - https://developers.google.com/apps-script/reference/url-fetch/http-response – Anton Barycheuski Mar 23 '14 at 14:45
  • 1
    Unterminated string literal means at some point you have something that breaks the string continuity, like a return carrier or something, or a special or broken char. Sanitize your output, and try to validate it using some json validator online, like http://jsonlint.com/ – Marco Jan 04 '17 at 13:23

0 Answers0