I want to download a JSON and parse it. I tried the following approach:
var request = require('request');
var url = "http://iiif.nli.org.il/collections/danhadani.json"
var result = request(url , function(error, response, body) {
console.log("Fin");
JSON.parse(body);
});
undefined
> Fin
Fin
SyntaxError: Unexpected token
at Object.parse (native)
at Request._callback (repl:1:81)
at Request.self.callback (/home/artium/Projects/nlihack-team-m7/node_modules/request/request.js:186:22)
at emitTwo (events.js:87:13)
at Request.emit (events.js:172:7)
at Request.<anonymous> (/home/artium/Projects/nlihack-team-m7/node_modules/request/request.js:1163:10)
at emitOne (events.js:77:13)
at Request.emit (events.js:169:7)
at IncomingMessage.<anonymous> (/home/artium/Projects/nlihack-team-m7/node_modules/request/request.js:1085:12)
at IncomingMessage.g (events.js:260:16)
I am able to log the JSON string retrieved in the body, it looks ok to me, so I guess I am doing the parsing wrong.
Edit:
The first characters of the body looks like this:
> body.substring(1,250)
'{"@context":"http://iiif.io/api/presentation/2/context.json",\n"@id": "http://iiif.nli.org.il/collections/danhadani.json",\n"@type":"sc:Collection",\n"label":"Dan Hadani Collection", \n"attribution":[{"@value":"The National Library of Israel","@language'
console.log(body)withconsole.log("Fin")JSON.parse(body)should work fine. If it doesn't, there is likely a syntax error somewhere in the JSON. You might run it against an online parser and see what it says.