Reputation: 5329
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'
Upvotes: 0
Views: 61
Reputation: 2377
Try this:
var request = require('request');
var url = "http://iiif.nli.org.il/collections/danhadani.json";
var options = {
uri: url,
method: 'GET',
json : true,
encoding: 'utf8'
};
var r = request(options , function(error, response, body) {
console.log("Fin");
// now you have an Array(43515) of objects on body.members without the need of parsing.
console.log(`The first object in the json file is: ${body.members[0]}`);
});
You will get the data as array of objects (becuse of the format of that json file)
I tryed the code, and it works.
בהצלחה!
Upvotes: 1
Reputation: 2479
Tested & working after specifying encoding:
{encoding:'utf8'}
It seems that the specific url you are requesting does not specificity it's encoding in the response header, so we have to manually set it like so:
request(u ,{encoding:'utf8'},
function(error, response, body) { console.log("Fin"); JSON.parse(body) })
Upvotes: 3