jogge
jogge

Reputation: 15

get specific data from https body

I want to get the median_price value from link

My code:

const https = require('https');
var item = ('M4A1-S | Decimator (Field-Tested)')
var body = '';
https.get('https://steamcommunity.com/market/priceoverview/?appid=730&market_hash_name=' + item, res => {
    res.on('data', data => {
        body += data;})
    res.on('end', () => console.log(body));
}).on('error', error => console.error(error.message));

output is:

{"success":true,"lowest_price":"$5.17","volume":"469","median_price":"$5.09"}

I want it to be:

5.09

so I can add multiple values together. What do I need to change?

Upvotes: 1

Views: 36

Answers (1)

Matus Dubrava
Matus Dubrava

Reputation: 14462

Turn the result into javascript object using JSON.parse, access it as usual and turn it to number using parseFloat (you can use substr method to remove the leading $)

parseFloat(JSON.parse(body).median_price.substr(1))

so in your code

res.on('end', () => console.log(parseFloat(JSON.parse(body).median_price.substr(1)));

Upvotes: 1

Related Questions