Home > OS >  Trying to process json file returned from an API call in NodeJS - error: undefined - why?
Trying to process json file returned from an API call in NodeJS - error: undefined - why?

Time:07-02

I am receiving a JSON file from an APIcall (see below). Now, I would like to access the different keys of the JSON file to present the various values separately. However, I get an 'undefined' message in the console. Any ideas?

This is the code to call the API and process JSON file:

   const input = req.body.var_1;
    console.log('Request Query:'   JSON.stringify({ "user_input": input}));
    const articles = [];
    const sent = 'test';
    const api_url = '...';
    const options = {
        method: 'POST',
        body: JSON.stringify({"user_input": input}),
        headers: {'Content-Type': 'application/json'}
    }

    const response = await fetch(api_url, options);
    const results = await Promise.all([response.json()]);
    console.log(results);
    data = JSON.parse(JSON.stringify(results))
    console.log(data);
    console.log(data.Topic);

This is the console output I get:

Request Query:{"user_input":"xgboost"}
[
  {
    Topic: {
      '0': 'random forest , Machine Learning and Wine Quality: Finding a good wine using multiple classifications',
      '1': 'decision tree learning , Gradient Boost for Classification',
      '2': 'feature selection , Understanding Multilabel Text Classification and the related process',
      '3': 'decision tree learning , Gradient Boost for Regression',
      '4': '<strong >Understanding AdaBoost</strong> , Anyone starting to learn Boosting technique should start first with AdaBoost or…'
    },
    'URL/PDF': {
      '0': 'https://dev.to//leading-edje/machine-learning-and-wine-quality-finding-a-good-wine-using-multiple-classifications-4kho',
      '1': 'https://dev.to//xsabzal/gradient-boost-for-classification-2f15',
      '2': 'https://dev.to//botreetechnologies/understanding-multilabel-text-classification-and-the-related-process-n66',
      '3': 'https://dev.to//xsabzal/gradient-boost-for-regression-1e42',
      '4': 'https://towardsdatascience.com/understanding-adaboost-2f94f22d5bfe'
    },
    Doc_Type: {
      '0': 'article',
      '1': 'article',
      '2': 'article',
      '3': 'article',
      '4': 'article'
    }
  }
]
[
  {
    Topic: {
      '0': 'random forest , Machine Learning and Wine Quality: Finding a good wine using multiple classifications',
      '1': 'decision tree learning , Gradient Boost for Classification',
      '2': 'feature selection , Understanding Multilabel Text Classification and the related process',
      '3': 'decision tree learning , Gradient Boost for Regression',
      '4': '<strong >Understanding AdaBoost</strong> , Anyone starting to learn Boosting technique should start first with AdaBoost or…'
    },
    'URL/PDF': {
      '0': 'https://dev.to//leading-edje/machine-learning-and-wine-quality-finding-a-good-wine-using-multiple-classifications-4kho',
      '1': 'https://dev.to//xsabzal/gradient-boost-for-classification-2f15',
      '2': 'https://dev.to//botreetechnologies/understanding-multilabel-text-classification-and-the-related-process-n66',
      '3': 'https://dev.to//xsabzal/gradient-boost-for-regression-1e42',
      '4': 'https://towardsdatascience.com/understanding-adaboost-2f94f22d5bfe'
    },
    Doc_Type: {
      '0': 'article',
      '1': 'article',
      '2': 'article',
      '3': 'article',
      '4': 'article'
    }
  }
]
undefined

Happy for any help, thx

CodePudding user response:

Note the [ ] in the output - results (or data, for that matter) is an array, so the Topic would be results[0].Topic (the array itself doesn't have a property Topic, only its element has one, hence the undefined result).

But: It shouldn't even be an array in the first place, it only is one because you make it one here:

const results = await Promise.all([response.json()]);

Promise.all will wait for an array of promises and return an array of their resolved values. You provide an array with one promise, so you will get back an array with one value, instead of getting only the value itself.

There is no point in using Promise.all with just one promise though, so you can get rid of that part and thereby also get rid of the array:

const results = await response.json()
console.log(results.Topic)

(As you can see, the whole JSON.parse(JSON.stringify(...)) shenanigans are superfluous as well.)

CodePudding user response:

It looks like you have the Object nested in an array so you have to access the first item in the array with data[0].Topic.

  • Related