Reputation: 43
Till "Sponsorship" - "impression_urls" is it parsing correctly but after that it crashed. am i doing anything wrong.? i am new in this platform. please help.
// Code start//
const express = require("express");
const bodyParser = require("body-parser");
const https = require("https");
const app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.get("/", function(req, res) {
res.sendFile(__dirname + "/index.html");
const url = "https://api.unsplash.com/photos/?client_id= my clinet id is given here";
https.get(url, function(response) {
console.log(response.statusCode);
response.on("data", function(data) {
const unspalshData = JSON.parse(data);
console.log(unspalshData.created_at);
});
});
});
// Code end //
Output got crashed after some parsing. like below -
// output start //
[nodemon] starting `node app.js`
server is running in port 3000
200
undefined:1
[{"id":"bXfQLglc81U","created_at":"2020-07-01T18:30:13-04:00","updated_at":"2020-07-07T01:16:02-04:00","promoted_at":null,"width":6016,"height":4016,"color":"#1A1519","description":null,"alt_description":"macbook pro on brown wooden table","urls":{"raw":"https://images.unsplash.com/photo-1593642533144-3d62aa4783ec?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjE0NzI5Nn0","full":"https://images.unsplash.com/photo-1593642533144-3d62aa4783ec?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NzI5Nn0","regular":"https://images.unsplash.com/photo-1593642533144-3d62aa4783ec?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjE0NzI5Nn0","small":"https://images.unsplash.com/photo-1593642533144-3d62aa4783ec?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NzI5Nn0","thumb":"https://images.unsplash.com/photo-1593642533144-3d62aa4783ec?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=200&fit=max&ixid=eyJhcHBfaWQiOjE0NzI5Nn0"},"links":{"self":"https://api.unsplash.com/photos/bXfQLglc81U","html":"https://unsplash.com/photos/bXfQLglc81U","download":"https://unsplash.com/photos/bXfQLglc81U/download","download_location":"https://api.unsplash.com/photos/bXfQLglc81U/download"},"categories":[],"likes":50,"liked_by_user":false,"current_user_collections":[],"sponsorship":{"impression_urls":
SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at IncomingMessage.<anonymous> (C:\jowar_drive\Web_dev_bootcamp\Practise web dev\unspalshAPI\app.js:28:33)
at IncomingMessage.emit (events.js:315:20)
at addChunk (_stream_readable.js:295:12)
at readableAddChunk (_stream_readable.js:271:9)
at IncomingMessage.Readable.push (_stream_readable.js:212:10)
at HTTPParser.parserOnBody (_http_common.js:132:24)
at TLSSocket.socketOnData (_http_client.js:469:22)
at TLSSocket.emit (events.js:315:20)
at addChunk (_stream_readable.js:295:12)
[nodemon] app crashed - waiting for file changes before starting...
// output end //
Upvotes: 0
Views: 49
Reputation: 165
As per your post, the JSON string seems to be incorrect. Because it is not ending with the required braces. So, the JSON is not incomplete and that is why the parsing is giving you the error.
If you add ""}}] This braces and quotes at the end of your JSON string, it will work. But I am wondering how you are getting this incomplete JSON from the API. If you can provide the entire response of the API, that would be more helpful but as per the JSON you have posted, I gave my answer. Please check the screenshot below.
Upvotes: 1