split-json

split text stream (JSON sent through some kind of readable stream) into JSON objects

split-json

split JSON was sent through some kind of readable stream in JSON objects

***100% of code coverage (npm test & npm run coverage)

V1.7

stream.pipe(split_json([match], [encoding], [replace]))
.on('data', function (data) {
    console.log(split_json.JSONValid(data))
})

[match]    => RegExp, default to os.EOL
[encoding] => String, utf8/ascii, default to utf8
[replace]  => RegExp, enable you to remove some delimiters at begin and end of a big JSON file
    ex: /(^\[\n)|(\n\]\n$)/
    
    
// some emitters
on('data', callback(data))
on('end', [callback]);


// validates JSON, instead to use JSON.parse inside of a try/catch
split_json.JSONValid -> null in case of error, if ok return the object


// check is a valid Object
split_json.isObject(obj) -> true/false
// from file

var rs = fs.createReadStream('./test/file1.json')
rs.pipe(split_json([match])).on('data', callback)
    
    

// from mongoDB

var mongojs = require('mongojs');
var split_json = require('split-json');

var db = mongojs('str_conn');
var collection = db.collection('my_coll');

collection.find({})
.pipe(split_json())
.on('data', function (doc) {
    console.log(doc);
})
.on('end', function () {
    db.close();
});
      
  
  
// TCP socket

on('data', function (data) {
    var rs = new Readable();
    rs.push(data);
    rs.push(null);

    rs.pipe(split_json(/\n/)).on('data', function cb_data (data) {
      cb(null, data);
    });
});