0

I have got the response from the JSON API, but I don't know how to parse it, it just comes back with an error, I don't know enough about it to figure it out, it returns:

(node:36308) UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token o in JSON at position 1

var fetch = require('node-fetch');
fetch('https://sv443.net/jokeapi/v2/joke/Any', function(res){
    if (res.ok) {
        return res;
        } else {
        console.log(res.statusText);
    }
})
.then(res => res.json())
.then((json) => {
    var parsedData = JSON.parse(json)
    console.log(parsedData.joke);
});
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

3 Answers3

2

You just need to do the following to access the delivery.

fetch("https://sv443.net/jokeapi/v2/joke/Any?type=single")
  .then(response => {
    return response.json();
  })
  .then(json => {
    // likely to be json.delivery but cannot 
    // confirm until rate limits have been lifted
    console.log(JSON.stringify(json));
  })
  .catch(err => {
    console.log(err);
  });
Paul Fitzgerald
  • 11,770
  • 4
  • 42
  • 54
0

Try this:

fetch('https://sv443.net/jokeapi/v2/joke/Any', function(res){
    if (res.ok) {
        return res;
        } else {
        console.log(res.statusText);
    }
})
.then(response => response.json())
  .then(data => console.log(data));
Reem Alon
  • 11
  • 3
0

You are already parsing it with res.json(). It returns an object (in promise) which can be accessed directly. Depending on a type prop you may have different props to check for. For example twopart joke will have setup: question, and delivery: answer

Thomas
  • 26
  • 3