I'm trying to parse this data from Marvel API by using JSON.parse(); but I am not able to. I am using Node.js.
Here is the code which I am using to get data from the api:
app.post("/", function(req, res) {
const nameStartsWith = req.body.superhero;
const ts = "timestamp";
const apikey = "myPublicKey";
const hash = "md5 digest";
const url = "http://gateway.marvel.com/v1/public/characters?ts=" + ts +
"&apikey=" + apikey + "&hash=" + hash + "&nameStartsWith="
+ nameStartsWith;
http.get(url, function(response) {
response.on("data", function(data) {
// getting error here
console.log(JSON.parse(data));
});
});
});
Below is the incomplete JSON data:
{"code":200,"status":"Ok","copyright":"© 2022 MARVEL","attributionText":"Data provided by Marvel. © 2022 MARVEL","attributionHTML":"<a href=\"http://marvel.com\">Data provided by Marvel. © 2022 MARVEL</a>","etag":"08aea880d9e638f37cf6011d63a936fbe3f82f22","data":{"offset":0,"limit":20,"total":5,"count":5,"results":[{"id":1009718,"name":"Wolverine","description":"Born with super-human senses and the power to heal from almost any wound, Wolverine was captured by a secret Canadian organization and given an unbreakable skeleton and claws. Treated like an animal, it took years for him to control himself. Now, he's a premiere member of both the X-Men and the Avengers.","modified":"2016-05-02T12:21:44-0400","thumbnail":{"path":"http://i.annihil.us/u/prod/marvel/i/mg/2/60/537bcaef0f6cf","extension":"jpg"},"resourceURI":"http://gateway.marvel.com/v1/public/characters/1009718","comics":{"available":2549,"collectionURI":"http://gateway.marvel.com/v1/public/characters/1009718/comics","items":[{"resourceURI":"http://gateway.marvel.com/v1/public/comics/41113","name":"5 Ronin (Hardcover)"},{"resourceURI":"http://gateway.marvel.com/v1/public/comics/38756","name":"5 Ronin (2010) #1"
It is showing this error in my hyper terminal:
SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at IncomingMessage.<anonymous> (C:\Users\mishr\OneDrive\Desktop\MarvelComics-Project\app.js:79:24)