Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138249

Trying to filter out Javascript output

$
0
0

I am trying to filter the following output from my code to only show the last_price :

object
received from server :  {
  channel: 'ticker.BTC-PERPETUAL.raw',
  data: {
    timestamp: 1574052234394,
    stats: { volume: 16032.65231878, low: 8376, high: 8639 },
    state: 'open',
    settlement_price: 8422.52,
    open_interest: 87745021,
    min_price: 8342.85,
    max_price: 8596.95,
    mark_price: 8469.85,
    last_price: 8470.5,
    instrument_name: 'BTC-PERPETUAL',
    index_price: 8463.82,
    funding_8h: 0.00004718,
    current_funding: 0.00021244,
    best_bid_price: 8470,
    best_bid_amount: 140,
    best_ask_price: 8470.5,
    best_ask_amount: 275870
  }
}

This is the code that I am using:

const WebSocket = require('ws');
var ws = new WebSocket('wss://www.deribit.com/ws/api/v2'); 

// To subscribe to this channel:
var msg = 
    {"jsonrpc": "2.0",
     "method": "public/subscribe",
     "id": 42,
     "params": {
        "channels": ["ticker.BTC-PERPETUAL.raw"]}
    };
var ws = new WebSocket('wss://www.deribit.com/ws/api/v2');
ws.onmessage = function (e) {
    // do something with the notifications...
    var jason = JSON.parse(e.data);
    console.log(typeof(jason));
    console.log('received from server : ', jason['params']);
};
ws.onopen = function () {
    ws.send(JSON.stringify(msg));
};

I have tried using console.log('received from server : ', jason['params']['data']); However I get the error: TypeError: Cannot read property 'data' of undefined

Here is the screenshot requested by @phil@radha code run with just console.log(JSON.parse(e.data));


Viewing all articles
Browse latest Browse all 138249

Trending Articles