I have an issue with an API call from polygon.io.
I am using their free API which gives me information on stock tickers.
I am using an API that provides me a response when called.
From the response, I need to get response > data > results > "c" value, in this case, 1014.97
So far the best I could do is:
console.log(response.data.results)
which gives:
Array [
Object {
"T": "TSLA",
"c": 1014.97,
"h": 1090.5753,
"l": 1000.21,
"n": 1043576,
"o": 1084.79,
"t": 1638565200000,
"v": 30773995,
"vw": 1028.7486,
},
Please tell me how I can get the "c" value?
Here is the full response data:
console.log(response)
This, in VS-Code>Terminal shows:
Object {
"config": Object {
"adapter": [Function xhrAdapter],
"baseURL": "https://api.polygon.io/",
"data": undefined,
"headers": Object {
"Accept": "application/json",
},
"maxBodyLength": -1,
"maxContentLength": -1,
"method": "get",
"params": Object {
"apiKey": "Gp5IgTvT4fPbfhINPpOk7iBIUDjOTRc9",
},
"timeout": 5000,
"transformRequest": Array [
[Function transformRequest],
],
"transformResponse": Array [
[Function transformResponse],
],
"transitional": Object {
"clarifyTimeoutError": false,
"forcedJSONParsing": true,
"silentJSONParsing": true,
},
"url": "v2/aggs/ticker/MSFT/prev",
"validateStatus": [Function validateStatus],
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
},
"data": Object {
"adjusted": true,
"count": 1,
"queryCount": 1,
"request_id": "6a2a6c28efc5fcf9e4b1f295f845bf91",
"results": Array [
Object {
"T": "TSLA",
"c": 1014.97,
"h": 1090.5753,
"l": 1000.21,
"n": 1043576,
"o": 1084.79,
"t": 1638565200000,
"v": 30773995,
"vw": 1028.7486,
},
],
"resultsCount": 1,
"status": "OK",
"ticker": "MSFT",
},
"duration": 406,
"headers": Object {
"content-type": "application/json",
"date": "Sat, 04 Dec 2021 21:37:55 GMT",
"server": "nginx/1.19.2",
"strict-transport-security": "max-age=15724800; includeSubDomains",
"vary": "Accept-Encoding",
"x-request-id": "6a2a6c28efc5fcf9e4b1f295f845bf91",
},
"ok": true,
"originalError": null,
"problem": null,
"status": 200,
}
2
Answers
Since "results" is an array with a dictionary inside you can reference the close value with response.data.results[‘c’].
Results is an array of dictionary Object. The dictionary Object containing "c" value is the first elements of the array.
So, you can get it like this