EzDevInfo.com

JSONStream

rawStream.pipe(JSONStream.parse()).pipe(streamOfObjects)

Error installing cordova

After running this command:

sudo npm install -g cordova

I get this error:

npm ERR! notarget No compatible version found: JSONStream@'>=1.0.3-0 <2.0.0-0'
npm ERR! notarget Valid install targets:
npm ERR! notarget ["0.0.0","0.1.0","0.1.1","0.1.2","0.1.3","0.2.0","0.2.1","0.2.2","0.2.3","0.3.0","0.3.1","0.3.2","0.3.3","0.4.0","0.4.1","0.4.2","0.4.3","0.4.4","0.5.0","0.6.0","0.6.1","0.6.2","0.6.3","0.6.4","0.7.0","0.7.1","0.8.0","0.7.2","0.7.3","0.7.4","0.8.1","0.8.2","0.8.3","0.8.4","0.9.0","0.10.0"]
npm ERR! notarget 
npm ERR! notarget This is most likely not a problem with npm itself.
npm ERR! notarget In most cases you or one of your dependencies are requesting
npm ERR! notarget a package version that doesn't exist.

Help would be welcome. Thanks


Source: (StackOverflow)

Nodejs: JSONStream parse method regex

I have a payload like this

{
    "rows": [{
        "id": "1234",
        "data": {
            "updatedby": "uid1",
            "resource": {
                "resourceid": "abcd"
            }
        }
    }, {
        "id": "1235",
        "data": {
            "updatedby": "uid2",
            "resource": {
                "resourceid": "pqrs"
            }
        }
    }, {
        "id": "1236",
        "data": {
            "updatedby": "uid3",
            "resource": {
                "resourceid": "bert"
            }
        }
    }]
}

I need to extract the content of the RESOURCE tag only from the json payload. Can you please help me formulate the regex? The below is what I tried and it doesn't invoke the parser.data method.

var parser = JSONStream.parse(['rows', true, /^resource/]);

parser.on('data', function(data) {
    console.log('received the payload -do something');
});

Source: (StackOverflow)

Advertisements

JSON streaming with Oboe.js, MongoDB and Express.js

I'm experimenting with JSON streaming through HTTP with Oboe.js, MongoDB and Express.js.

The point is to do a query in MongoDB (Node.js's mongodb native drive), pipe it (a JavaScript array) to Express.js and parse it in the browser with Oboe.js.

The benchmarks I did compared streaming vs. blocking in both the MongoDB query server-side and the JSON parsing in the client-side.

Here is the source code for the two benchmarks. The first number is the number of milli-seconds for 1000 queries of 100 items (pagination) in a 10 million documents collection and the second number between parenthesis, represents the number of milli-seconds before the very first item in the MongoDB result array is parsed.

The streaming benchmark server-side:

// Oboe.js - 20238 (16.887)
// Native - 16703 (16.69)

collection
.find()
.skip(+req.query.offset)
.limit(+req.query.limit)
.stream()
.pipe(JSONStream.stringify())
.pipe(res);

The blocking benchmark server-side:

// Oboe.js - 17418 (14.267)
// Native - 13706 (13.698)

collection
.find()
.skip(+req.query.offset)
.limit(+req.query.limit)
.toArray(function (e, docs) {
    res.json(docs);
});

These results really surprise me because I would have thought that:

  1. Streaming would be quicker than blocking every single time.
  2. Oboe.js would be quicker to parse the entire JSON array compared to the native JSON.parse method.
  3. Oboe.js would be quicker to parse the first element in the array compared to the native JSON.parse method.

Does anyone have an explanation ? What am I doing wrong ?

Here is the source-code for the two client-side benchmarks too.

The streaming benchmark client-side:

var limit = 100;
var max = 1000;

var oboeFirstTimes = [];
var oboeStart = Date.now();

function paginate (i, offset, limit) {
    if (i === max) {
        console.log('> OBOE.js time:', (Date.now() - oboeStart));
        console.log('> OBOE.js avg. first time:', (
            oboeFirstTimes.reduce(function (total, time) {
                return total + time;
            }, 0) / max
        ));
        return true;
    }

    var parseStart = Date.now();
    var first = true;
    oboe('/api/spdy-stream?offset=' + offset + '&limit=' + limit)
    .node('![*]', function () {
        if (first) {
            first = false;
            oboeFirstTimes.push(Date.now() - parseStart);
        }
    })
    .done(function () {
        paginate(i + 1, offset + limit, limit);
    });
}

paginate(0, 0, limit);

The blocking benchmark client-side:

var limit = 100;
var max = 1000;

var nativeFirstTimes = [];
var nativeStart = Date.now();

function paginate (i, offset, limit) {
    if (i === max) {
        console.log('> NATIVE time:', (Date.now() - nativeStart));
        console.log('> NATIVE avg. first time:', (
            nativeFirstTimes.reduce(function (total, time) {
                return total + time;
            }, 0) / max
        ));
        return true;
    }

    var parseStart = Date.now();
    var first = true;

    var req = new XMLHttpRequest();
    req.open('GET', '/api/spdy-stream?offset=' + offset + '&limit=' + limit, true);

    req.onload = function () {
        var json = JSON.parse(req.responseText);
        json.forEach(function () {
            if (first) {
                first = false;
                nativeFirstTimes.push(Date.now() - parseStart);
            }
        });
        paginate(i + 1, offset + limit, limit);
    };

    req.send();
}

paginate(0, 0, limit);

Thanks in advance !


Source: (StackOverflow)

Basic test of JSONStream not working

I'm having a look at JSONStream in node.js, and I'm trying the following small app to get a handle on it:

var JSONStream = require('JSONStream');
var Stream = require('stream');

var s = new Stream();
s.pipe = function(dest) {
  dest.write('{"foo":1}');
  return dest;
};

var parser = JSONStream.parse(/foo/);
s.pipe(parser).pipe(process.stdout);

Unfortunately, when run on the commandline, this doesn't write anything to the console. What am I doing wrong?


Source: (StackOverflow)

Dealing with a JSON object too big to fit into memory

I have a dump of a Firebase database representing our Users table stored in JSON. I want to run some data analysis on it but the issue is that it's too big to load into memory completely and manipulate with pure JavaScript (or _ and similar libraries).

Up until now I've been using the JSONStream package to deal with my data in bite-sized chunks (it calls a callback once for each user in the JSON dump).

I've now hit a roadblock though because I want to filter my user ids based on their value. The "questions" I'm trying to answer are of the form "Which users x" whereas previously I was just asking "How many users x" and didn't need to know who they were.

The data format is like this:

{
    users: {
        123: {
            foo: 4
        },
        567: {
            foo: 8
        }
    }
}

What I want to do is essentially get the user ID (123 or 567 in the above) based on the value of foo. Now, if this were a small list it would be trivial to use something like _.each to iterate over the keys and values and extract the keys I want.

Unfortunately, since it doesn't fit into memory that doesn't work. With JSONStream I can iterate over it by using var parser = JSONStream.parse('users.*'); and piping it into a function that deals with it like this:

var stream = fs.createReadStream('my.json');

stream.pipe(parser);

parser.on('data', function(user) {
    // user is equal to { foo: bar } here
    // so it is trivial to do my filter
    // but I don't know which user ID owns the data
});

But the problem is that I don't have access to the key representing the star wildcard that I passed into JSONStream.parse. In other words, I don't know if { foo: bar} represents user 123 or user 567.

The question is twofold:

  1. How can I get the current path from within my callback?
  2. Is there a better way to be dealing with this JSON data that is too big to fit into memory?

Source: (StackOverflow)

How to parse an infinite json array from stdin in go?

I'm trying to write a small replacement for i3status, a small programm that comunicates with i3bar conforming this protocol. They exchange messeages via stdin and stdout.

The stream in both directions is an infinite array of json objects. The start of the stream from i3bar to i3status (which i want to replace) looks like this:

[
{"name": "some_name_1","instance": "some_inst_1","button": 1,"x": 213,"y": 35}
,{"name": "some_name_1","instance": "some_inst_2","button": 2,"x": 687,"y": 354}
,{"name": "some_name_2","instance": "some_inst","button": 1,"x": 786,"y": 637}
,{"name": "some_name_3","instance": "some_inst","button": 3,"x": 768,"y": 67}
...

This is an "array" of objects which represent clicks. The array will never close.

My question is now: What is the right way of parsing this?

Obviously I cannot use the json library because this is not a vaild json object.


Source: (StackOverflow)

Node.js - Allocation failed - process out of memory even with use of JSONStream parser

I'm trying to read and parse through about 12 large (ranging from 100mb+ to 500mb+) JSON files in node. I tried using JSONStream (as suggested by many others as a solution for this problem) to prevent the following error:

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory

However, I'm still getting this error. This is the first time I've ever tried using a streaming file reader like this, so I'm not sure what could be the problem. Right now, the code I have is:

for (var i = 0; i < jsonFileArray.length; i++) {
    if (jsonFileArray[i].match(/\.json$/)) {
        var stream = fs.createReadStream(dirPath + jsonFileArray[i]).pipe(JSONStream.parse(['records', true, 'Categories']));

      stream.on('data', function(data) {
          console.log('received:', data);
        });
    }
}

Eventually I want to build a cumulative result object with all the parsed data from these files, but so far everything I've tried has prevented me from being able to parse through any and all of the files. Suggestions? Guidance?


Source: (StackOverflow)

JSONStream handle one data with different parser

I'm using JSONStream to parse the data from server, the data can either be like {"error": "SomeError"} or {"articles":[{"id": 123}]};

My code goes like

var request = require('request');
var JSONStream = require('JSONStream');

var articleIDParser = JSONStream.parse(['articles', true, 'id']);
var errorParser = JSONStream.parse(['error']);

request({url: 'http://XXX/articles.json'})
    .pipe(articleIDParser).pipe(errorParser);

errorParser.on('data', function(data) {
    console.log(data);
});

articleIDParser.on('data', someFuncHere);

But unlucky, the second parser does not work even when the server returns error.

Am I wrong at pipe function or JSONStream?

Thanks in advance.


Source: (StackOverflow)