0

I have a url that returns JSON, but the beginning of the return string is

])}while(1);</x>{"success":true,"payload":{"value":

I want to split on ])}while(1);</x> and look at the [1] value of the that split array.

Right now I'm doing

fetch(jsonURL)
  .then(res => {
    console.log(res); 
  });

Normally I'd split at the console.log but I'm getting the following error:

the-myth-of-the-genius-programmer-9381a884591e:1 Uncaught (in promise) SyntaxError: Unexpected token ] in JSON at position 0

melpomene
  • 79,257
  • 6
  • 70
  • 127
Zack Shapiro
  • 5,202
  • 14
  • 65
  • 121

1 Answers1

5

You need to call response.text() to get the raw text of the response, so you can remove the extraneous stuff at the beginning. Then you have to parse the JSON yourself, you can't use res.json().

fetch(jsonURL)
  .then(res => res.text())
  .then(text => {
    text = text.replace('])}while(1);</x>', '');
    let obj = JSON.parse(text);
    console.log(obj);
|);

This assumes the extraneous stuff is only at the beginning. If there's also something after the JSON, you'll need to remove that as well.

Barmar
  • 596,455
  • 48
  • 393
  • 495