NodeJS JSON error

New to asynchronous programming here.

I am trying to get JSON data and parse it in node. However, for some reason I keep getting the error:

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: JSON.parse is not a function

fetch(url).then(function(res) {
    return res.json();
}).then(function(json) {
    jsonObj=JSON.parse(json);
    console.log(json);
    console.log("HELLO");
    console.log(jsonObj.id);
});

      

JSON data when jsonObj=JSON.parse(json);

commented out:

[ { id: 1,
    url: 'http://www.tvmaze.com/shows/1/under-the-dome',
    name: 'Under the Dome',
    type: 'Scripted',
    language: 'English',
    genres: [ 'Drama', 'Science-Fiction', 'Thriller' ],
    status: 'Ended',
    runtime: 60,
    premiered: '2013-06-24',
    schedule: { time: '22:00', days: [Object] },
    rating: { average: 6.6 },
    weight: 0,
    network: { id: 2, name: 'CBS', country: [Object] },
    webChannel: null,
    externals: { tvrage: 25988, thetvdb: 264492, imdb: 'tt1553656' },
    image: 
     { medium: 'http://static.tvmaze.com/uploads/images/medium_portrait/0/1.jpg',
       original: 'http://static.tvmaze.com/uploads/images/original_untouched/0/1.jpg' },
    summary: '<p>Inhabitants must deal with surviving the post-apocalyptic conditions</p>',
    updated: 1490211618,
    _links: { self: [Object], previousepisode: [Object] } }, ... 140 more items

      

+3


source to share


1 answer


Your error indicates that you have overridden the JSON object itself - something like this;

$ node
> a='{"a":7}'
'{"a":7}'
> JSON.parse(a)
{ a: 7 }
> JSON=7
7
> JSON.parse(a)
TypeError: JSON.parse is not a function

      



Check that the value is JSON and JSON.parse and I am pretty sure it is not a function

0


source







All Articles