Seeking information from an API, my goal is to extract specific data using JavaScript selectors. I am specifically interested in two objects from the JSON below:
[
{
"symbol": {
"tickerSymbol": "@CH20",
"vendor": "DTN",
"marketName": "cbot",
"symbol": "@C@1",
"description": "CORN March 2020"
},
"last": {
"number": 380.25
},
"high": {
"number": 382.75
},
"low": {
"number": 379.5
},
"open": {
"number": 382.25
},
"bid": {
"number": 380
},
"ask": {
"number": 380.25
},
"close": null,
"previous": {
"number": 383
},
"cumVolume": 80553,
"openInterest": 428574,
"change": {
"number": -2.75
},
"week52High": null,
"week52Low": null,
"month": "Mar 20",
"settleDate": "2020-02-12",
"settlePrice": {
"number": 383
},
"expirationDate": "2020-03-13",
"contractHigh": {
"number": 476
},
"contractLow": {
"number": 365.75
},
"quoteDelay": 10,
"bidDateTime": "2020-02-13T10:40:12-06:00",
"askDateTime": "2020-02-13T10:40:12-06:00",
"tradeDateTime": "2020-02-13T10:39:31-06:00"
}
]
The following code snippet demonstrates how I am utilizing JSON.parse
:
var data = JSON.parse(this.response)
if (request.status >= 200 && request.status < 400)
{
var last = data.last.number
var change = data.change.number
console.log(last);
console.log(change);
console.log(data);
}