1

I'm using Node to request API and parse the returned JSON. When I requests an API for several times, node crashes and reports a SyntaxError: Unexpected token error. The error is located at a character in a string of the JSON object. I found that the string is very long (60000+ characters encoded with utf-8). Is this problem caused by the too long string? How does it happens?

JSON string can see here: http://www.liyan93.com/mdnotes/JinquanquanIssues/errorData.json

3
  • 1
    This parsed for me with no problem. Commented May 26, 2015 at 18:40
  • 1
    This string isn't terribly long. I've dealt with strings 10x this file. Perhaps the unicode is being an issue for you. Commented May 26, 2015 at 18:41
  • @TomDDD Seems you're right. But why will this problem happen after several times instead of every time? Commented May 27, 2015 at 11:00

2 Answers 2

2
https.get(url, (resp) => {
    let data = '';

    // A chunk of data has been recieved.
    resp.on('data', (chunk) => {
      data += chunk;
    });

    // The whole response has been received. Print out the result.
    resp.on('end', () => {
      const jsonDATA = JSON.parse(data).parse;

      var jsonText = jsonDATA.text;

      console.log(jsonText);
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

I have experienced this when the connection was terminated by the other side / cut off before the full string was transferred. Perhaps that could be the cause?

Tip: The latest Node provides a little more useful error upon json parse error, showing a part of the string. You can also use jju as the parser, it provides even a better error message.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.