NodeJS deserialization from a stream-Collection of common programming errors

I am having an issue deserializing from a stream in node (specifically the pricing feed from the Bitcoin GOX exchange). Basically a chunk arrives which is well formed complete and verified JSON. Here is the code:

var gox = require('goxstream');
var fs = require('fs');
var options = {
    currency: 'AUD',
    ticker: true,
    depth: false
};

var goxStream = gox.createStream(options);
goxStream.on('data', function(chunk) {

    console.log(JSON.parse(chunk));
});

When trying to parse it I get the following

undefined:0

^
SyntaxError: Unexpected end of input

Any ideas? I have included a sample chunk:

> {"channel": "eb6aaa11-99d0-4f64-9e8c-1140872a423d", "channel_name":
> "ticker.BTCAUD", "op": "private", "origin": "broadcast", "private":
> "ticker", "ticker": {
>     "high": {
>         "value": "121.51941",
>         "value_int": "12151941",
>         "display": "AU$121.51941",
>         "display_short": "AU$121.52",
>         "currency": "AUD"
>     },
>     "low": {
>         "value": "118.00001",
>         "value_int": "11800001",
>         "display": "AU$118.00001",
>         "display_short": "AU$118.00",
>         "currency": "AUD"
>     },
>     "avg": {
>         "value": "119.58084",
>         "value_int": "11958084",
>         "display": "AU$119.58084",
>         "display_short": "AU$119.58",
>         "currency": "AUD"
>     },
>     "vwap": {
>         "value": "119.80280",
>         "value_int": "11980280",
>         "display": "AU$119.80280",
>         "display_short": "AU$119.80",
>         "currency": "AUD"
>     },
>     "vol": {
>         "value": "249.73550646",
>         "value_int": "24973550646",
>         "display": "249.73550646\u00a0BTC",
>         "display_short": "249.74\u00a0BTC",
>         "currency": "BTC"
>     },
>     "last_local": {
>         "value": "118.50000",
>         "value_int": "11850000",
>         "display": "AU$118.50000",
>         "display_short": "AU$118.50",
>         "currency": "AUD"
>     },
>     "last_orig": {
>         "value": "108.99500",
>         "value_int": "10899500",
>         "display": "$108.99500",
>         "display_short": "$109.00",
>         "currency": "USD"
>     },
>     "last_all": {
>         "value": "118.79965",
>         "value_int": "11879965",
>         "display": "AU$118.79965",
>         "display_short": "AU$118.80",
>         "currency": "AUD"
>     },
>     "last": {
>         "value": "118.50000",
>         "value_int": "11850000",
>         "display": "AU$118.50000",
>         "display_short": "AU$118.50",
>         "currency": "AUD"
>     },
>     "buy": {
>         "value": "118.50000",
>         "value_int": "11850000",
>         "display": "AU$118.50000",
>         "display_short": "AU$118.50",
>         "currency": "AUD"
>     },
>     "sell": {
>         "value": "119.99939",
>         "value_int": "11999939",
>         "display": "AU$119.99939",
>         "display_short": "AU$120.00",
>         "currency": "AUD"
>     },
>     "item": "BTC",
>     "now": "1376715241731341" }}

You can verify it here: http://jsonlint.com

Also it is probably worth mentioning I have already tried parsing and removing the escaped characters. Also have tried a couple of different serializers with the same results

  1. You are getting the data chunk by chunk. Chunks themselves may not be complete JSON objects. Either buffer all of the data, or use something to do it for you (say the request module), or if you need to parse a long stream take a look at the JSONparse module.

Originally posted 2013-11-19 13:17:31.