I am trying to parse a response from the YouTube Data API 3 into my node.js project. When I call JSON.parse(), it stops part way through the JSON and throws an "Unexpected end of JSON input" error.
I cam new to node.js and the YouTube Data API, but I was unable to find any issues with the JSON that was returned that could cause this error.
My request code:
https.get('https://www.googleapis.com/youtube/v3/search?part=snippet&q=' + gameSearch + '+trailer&type=video&videoCaption=closedCaption&key=Google_API_Key', (res) => {
res.on('data', (d) => {
var result = JSON.parse(d.toString('utf8');
console.log(result);
response data (Example only for context):
{
"kind": "youtube#searchListResponse",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/c5qJp8_01Z7pygk7E4FxXPySdrE\"",
"nextPageToken": "CAUQAA",
"regionCode": "US",
"pageInfo": {
"totalResults": 834115,
"resultsPerPage": 5
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/Kd8M7QMYXGvUuq22Gmm8tSsH2Og\"",
"id": {
"kind": "youtube#video",
"videoId": "QpvM9uwOcUc"
},
"snippet": {
"publishedAt": "2011-06-09T13:13:41.000Z",
"channelId": "UC64oAui-2WN5vXC7hTKoLbg",
"title": "Elder Scrolls 5 : Skyrim Official Trailer",
"description": "Elder Scrolls 5 Skyrim - E3 2011 Trailer. Join us on Facebook : http://facebook.com/GameNewsOfficial. Subscribe Now : http://REMOVED DUE TO POSTING ERROR ! Bethesda ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/QpvM9uwOcUc/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/QpvM9uwOcUc/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/QpvM9uwOcUc/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "GameNewsOfficial",
"liveBroadcastContent": "none"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/9ufokW0y_tAzTii-9cOHe9z-V9M\"",
"id": {
"kind": "youtube#video",
"videoId": "JSRtYpNRoN0"
},
"snippet": {
"publishedAt": "2011-02-23T20:12:28.000Z",
"channelId": "UCvZHe-SP3xC7DdOk4Ri8QBw",
"title": "The Elder Scrolls V: Skyrim - Official Trailer",
"description": "We're pleased to share the first ever in-game trailer for The Elder Scrolls V: Skyrim, the next installment in The Elder Scrolls series from award-winning creators ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/JSRtYpNRoN0/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/JSRtYpNRoN0/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/JSRtYpNRoN0/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "Bethesda Softworks",
"liveBroadcastContent": "none"
}
},
Error that I keep receiving:
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/9ufokW0y_tAzTii-9cOHe9z-V9M\"
SyntaxError: Unexpected end of JSON input
at Object.parse (native)
at IncomingMessage.https.get.res.on (/home/ubuntu/workspace/oldFile.js:289:23)
at emitOne (events.js:96:13)
at readableAddChunk (_stream_readable.js:176:18)
at IncomingMessage.Readable.push (_stream_readable.js:134:10)
at HTTPParser.parserOnBody (_http_common.js:129:22)
at TLSSocket.socketOnData (_http_client.js:361:20)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:188:7)
I can see it seems to be parsing up until the second element in the "items" array and I cannot figure out why it's breaking!!
Cheers
via Djinné22
No comments:
Post a Comment