alireza
alireza

Reputation: 557

Node.js JSON.parse Unexpected token error

I have to import content for Json url in other site. (its almost more than 100K content)

here code i use (i am using requset for download josn file, and mongodb for saving data)

request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
var content = JSON.parse(body);
    // ....
    }
})

Everything work well but after 50K content imoprted i got this error

SyntaxError: Unexpected token <
    at Object.parse (native)
    at Request._callback (/home/app/app.js:58:30)
    at Request.self.callback (/home/app/node_modules/request/request.js:360:22)
    at Request.emit (events.js:98:17)
    at Request.<anonymous> (/home/app/node_modules/request/request.js:1202:14)
    at Request.emit (events.js:117:20)
    at IncomingMessage.<anonymous> (/home/app/node_modules/request/request.js:1150:12)
    at IncomingMessage.emit (events.js:117:20)
    at _stream_readable.js:944:16
    at process._tickCallback (node.js:448:13)

i will be appreciated if tell where i am wrong.

Upvotes: 1

Views: 1179

Answers (1)

dthree
dthree

Reputation: 20730

What is the source of the JSON file? Is it reliable? Review the JSON that is being parsed - "<" is very unusual for JSON, so it's probably not properly escaped.

I would also put a try { } catch(e) { } statement around your JSON.parse(body);

Upvotes: 1

Related Questions