Tuesday, 30 May 2017

Parsing Coinmarketcap JSON array in nodeJS

I'm developing a website to display some cryptocurrencies. Some of these I'm getting from Coinmarkepcap API (https://api.coinmarketcap.com/v1/ticker/).

The nodeJS code I'm using is the following:

var https = require('https'); 

https://api.coinmarketcap.com/v1/ticker/
var optionsget = {
    host : 'api.coinmarketcap.com', 
    port : 443,
    path : '/v1/ticker/bitcoin', 
    method : 'GET'
};

var reqGet = https.request(optionsget, function(res) {

    res.on('data', function(d) {
        info = JSON.parse(d);
        console.log(info);
    });
});

reqGet.end();
reqGet.on('error', function(e) {
    console.error(e);
});

I'm getting the following error:

SyntaxError: Unexpected token < in JSON at position 0

I noticed that the result from the API is using a bracket [] with the JSON inside. How can I parse the JSON so I can retrieve the name, price, id, etc?



via wiwa1978

No comments:

Post a Comment