1

I am trying to parse a JSON to get a specific line. I have tried many different things on google that I could find and this is as close as I can get.

What im trying to read:

[{
    "id": "pinkcoin", 
    "name": "PinkCoin", 
    "symbol": "PINK", 
    "rank": "321", 
    "price_usd": "0.0281999", 
    "price_btc": "0.00000165", 
    "24h_volume_usd": "195433.0", 
    "market_cap_usd": "10470475.0", 
    "available_supply": "371294750.0", 
    "total_supply": "388294750.0", 
    "max_supply": null, 
    "percent_change_1h": "5.48", 
    "percent_change_24h": "10.83", 
    "percent_change_7d": "-7.62", 
    "last_updated": "1513043947"
}]

I am trying to pull the "price_usd" part from this... Below is the code im using:

var request = require('request');
request('https://api.coinmarketcap.com/v1/ticker/pinkcoin/', function (error, response, body) {
  fs.readFile(body, 'utf8', function (err, data) {
    if (err) {
      console.log('Error: ' + err);
      return;
    }

    data = JSON.parse(data);

    bot.sendMessage({
      to: channelID,
      message: data.price_usd
    });
  });
});

But when I trigger this code to happen, I get this in console:

Error: Error: ENAMETOOLONG: name too long, open '[{
    "id": "pinkcoin",
    "name": "PinkCoin",
    "symbol": "PINK",
    "rank": "319",
    "price_usd": "0.0284066",
    "price_btc": "0.00000166",
    "24h_volume_usd": "195093.0",
    "market_cap_usd": "10547221.0",
    "available_supply": "371294750.0",
    "total_supply": "388294750.0",
    "max_supply": null,
    "percent_change_1h": "6.15",
    "percent_change_24h": "11.55",
    "percent_change_7d": "-6.97",
    "last_updated": "1513044245"
}]'

I've been looking to solve this but im getting nowhere...

1 Answer 1

1

I'm not sure why you're using fs.readFile there. You're taking the result of your api call, the whole JSON object returned, and using it as the path argument to fs.readFile. That JSON object is a really really long string, and it's longer than a file path is allowed to be, hence ENAMETOOLONG being thrown.

The body value from request should already have the JSON you want. Unless you wanted to read some file from your file system based on a value in the response from coinmarketcap's API, then remove the portion using fs.

EDIT: Also, as a bonus, you aren't using the result correctly either. It's returning an array, where the first object has your result. I'm not entirely sure under which circumstances it returns multiple values. So you'd want this:

bot.sendMessage({
  to: channelID,
  message: data.price_usd
});

... to look like this:

bot.sendMessage({
  to: channelID,
  message: data[0].price_usd
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.