Uncaught SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data

I have an array generated by the server:

const videos = ["1767504148", "1827943844", "227040308", "1258959982", "550360757", "31439346", "2125342928", "674399863", "713401082", "1698423836"];

For every item in this array I want to do an action so I have a foreach loop:

    videos.foreach(create)
    function create(number, id)
    {
        more code...

However, this gave me an error that the array had to be parsed so I decoded it:

    var vi = JSON.parse(videos)
    vi.foreach(create)
    function create(number, id)
    {
        more code...

This then gave me this error:

Uncaught SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 11 of the JSON data

How can I fix this?

const videos = ["1767504148", "1827943844", "227040308", "1258959982", "550360757", "31439346", "2125342928", "674399863", "713401082", "1698423836"];

is not valid JSON. Either run this as js or have the server output json

Agreed, isn’t this an array? When I tried to run it as an array I got videos.forEach is not a function

the server has passed you js code for an array. It can’t be json parsed without removing the const videos = . It can be run as js though

Turns out I was using the foreach function VS the forEach function. (thanks @wh0)

Oh yeah that would do it

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.