How do I get the price from the body key using NodeJS coinbase pro api? I am trying to get the price from the body key using the api command
getProductTicker
Below is what I have so far, but I don't know how to parse the price from the body key.
const CoinbasePro = require('coinbase-pro');
const publicClient = new CoinbasePro.PublicClient();
publicClient.getProductTicker('ETH-USD', function(err, obj) {
console.log('ETH-USD it worked');
console.log('Start of Entries\n');
console.log(Object.entries(obj));
console.log('End of Entries \n');
console.log('Start of keys\n');
console.log(Object.keys(obj));
console.log('End of keys \n');
});
Below is the output. I am trying to get the price from the body key.
_ended: true,
_callbackCalled: true
}
],
[ 'toJSON', [Function: responseToJSON] ],
[ 'caseless', Caseless { dict: [Object] } ],
[
'body',
'{"trade_id":53067029,"price":"125.24","size":"0.07555643","time":"2019-12-
27T17:04:37.429725Z","bid":"125.23","ask":"125.24","volume":"55843.38198278"}'
]
]
End of Entries
Start of keys
[
'_readableState', 'readable',
'_events', '_eventsCount',
'_maxListeners', 'socket',
'connection', 'httpVersionMajor',
'httpVersionMinor', 'httpVersion',
'complete', 'headers',
'rawHeaders', 'trailers',
'rawTrailers', 'aborted',
'upgrade', 'url',
'method', 'statusCode',
'statusMessage', 'client',
'_consuming', '_dumped',
'req', 'request',
'toJSON', 'caseless',
'body'
]
End of keys
You need to parse the body as it is coming as string. I am not sure if toJson
method could also be used or not but worth trying. try this.
const CoinbasePro = require("coinbase-pro");
const publicClient = new CoinbasePro.PublicClient();
publicClient.getProductTicker("ETH-USD", function(err, obj) {
const parsedBody = JSON.parse(obj.body);
const price = parsedBody.price;
});