finnhubio / Finnhub-API

Finnhub API provides institutional-grade financial data to investors, fintech startups and investment firms. We support real-time stock price, global fundamentals and alternative data. https://finnhub.io/docs/api
97 stars 6 forks source link

Extracting specific data from console.log(data) function using Javascript #454

Open rms2256 opened 3 years ago

rms2256 commented 3 years ago

I would like to know how to extract the data from a function call such as Symbol look-up or using the Basic Financials function through the Finnhub API.

I am programming in Javascript. An example of what I plan to do is below:

  1. The Javascript code for the symbol lookup function is as follows:
`const finnhub = require('finnhub');

const api_key = finnhub.ApiClient.instance.authentications['api_key'];
api_key.apiKey = "c4baobqad3i848j280rg"
const finnhubClient = new finnhub.DefaultApi()

finnhubClient.companyBasicFinancials("AAPL", "all", (error, data, response) => {
  console.log(data)
});`
  1. My attention is to the console.log(data) line on the second to last line of the code snippet. It prints the presumed data to the console, along with information about each result as shown below:

    SymbolLookup { result: [ SymbolLookupInfo { description: 'Barclays plc', displaySymbol: '31PD.L', symbol: '31PD.L', type: '' }, SymbolLookupInfo { description: 'SAGA PLC', displaySymbol: '65J.SG', symbol: '65J.SG', type: 'Common Stock' }, SymbolLookupInfo { description: 'Aviva PLC', displaySymbol: '45MZ.L', symbol: '45MZ.L', type: ''

    1. What I plan to do, is extract the information with in the results, such as the stock type or symbol and save it to a variable for further use. However, I do not know how to. I have researched such functions such as console.dir(data), but found no difference in output or how to save the specific information from the data set.

      Any productive help with this would be appreciated.

      Thank you,

rms2256 commented 3 years ago

I have solved this. The solution is to append the correct response attribute (listed with each function in the 'documentation' section of the website - for JavaScript) with the correct identifier.

For example, if I were to intending to extract the specific phone number of a stock using the 'Company Profile 2' function, I would append the response attribute identifier of 'phone' to my console.log prompt. For example:

const finnhub = require('finnhub');

const api_key = finnhub.ApiClient.instance.authentications['api_key'];
api_key.apiKey = "---------------------------------------"
const finnhubClient = new finnhub.DefaultApi()

finnhubClient.companyProfile2({'symbol': 'AAPL'}, (error, data, response) => {
  console.log(data.phone)
});