0

I'm using Google App Scripts for retrieve the information of a YouTube page.

I'd managed to retrieve the desired JSON object - embebed in the website's source code, but, after retrieving the JSON object (when I try to parse the JSON), I get the following error:

Uncaught SyntaxError: Invalid or unexpected token

I tried:

The string value (i.e, the JSON) is a valid one - I've checked it in beautifier.io and jsoneditoronline.org - which contains the formatted JSON data.

This is the jsfiddle with the obtained JSON - the JSON object is too large for add it in the body of my question, hence, the code is available in jsfiddle:

I'm unable to parse the JSON object by code (programatically) - I have to manually remove line breaks.

I can share the code I use for retrieve the JSON object from the website, but, I think the process to get the info is not the main issue, however, if needed, I can post the code.

What other alternatives I have to parse the JSON string in a JSON object (that can be used) programatically?

  • That's not JSON. What you've got in that fiddle is a plain JavaScript object initializer. There is no need to parse it. – Pointy Jul 23 '21 at 14:15
  • 1
    The fiddle code has a bunch of newlines in it that shouldn't be there and is making it invalid. You can spot them by the line numbers that jsfiddle provides. Removing them makes the [fiddle work](https://jsfiddle.net/Lg28kcz5/) – James Jul 23 '21 at 14:28
  • @Pointy if I don't parse it, I'm unable to get the info in the JSON, like: `JSON_obj[0].compactAutoplayRenderer.contents[0].videoWithContextRenderer.videoId` where `JSON_obj` is the parsed JSON. Can you give me some light for understand how I can use the plain JavaScript object initializer as I intended? – Marco Aurelio Fernandez Reyes Jul 23 '21 at 14:29
  • @James I don't know how else I can remove those newlines. – Marco Aurelio Fernandez Reyes Jul 23 '21 at 14:30
  • Try https://stackoverflow.com/questions/10805125/how-to-remove-all-line-breaks-from-a-string – James Jul 23 '21 at 14:34
  • 1
    Something about the way you're scraping that code is introducing extraneous newlines into one or more of the string property values, and (for `"..."`) strings those are invalid. You cannot fix the initializer, because the syntax error will happen before any of your code can run. Instead, I'd concentrate on what's going wrong when you scrape the content in the first place. If you have the stuff loaded into beautifier, you should be able to click the "Beautify" button and then copy/paste the resulting JavaScript code from there. – Pointy Jul 23 '21 at 14:43
  • Thank you everyone for your comments, after more tests, the problem is, indeed, with the way the scraping is made - since this is made by Google App Scripts, I have no way to change its behaviour; best I can do so far is handle the exception(s) when that appears while getting the JSON data - by using `JSON.parse(string_scraped)`. I think the question should stay open, but, itf it's closed, I wont have any issue with that. If I found a way to "fix" this "bug", I'll post it as an answer. – Marco Aurelio Fernandez Reyes Jul 23 '21 at 23:14

0 Answers0