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


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

stream.pipe(split([match], [encoding], [replace]))
.on('data', function (data) {
// js objects 
.on('data', function (obj) {
  • [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$)/)

on('data', callback(data)) on('end', [callback])

validates JSON, instead to use JSON.parse inside of a try/catch

split.JSONValid(obj) -> return object {value, error}

  • value:* should be the object corresponding to the given JSON text or undefined in case of error
  • error: should be an Error object in case of error or undefined in case of success

check is a valid Object split.isObject(obj) -> return boolean

var split = require('split-json')
// from file 
var rs = fs.createReadStream('./test/file1.json')
rs.pipe(split([match])).on('data', callback)
// for mongoDB with mongojs use split#obj 
var mongojs = require('mongojs')
var db = mongojs('str_conn')
var collection = db.collection('my_coll')
.on('data', function (doc) {
.on('end', function () {
// TCP socket 
on('data', function (data) {
    var rs = new Readable()
    rs.pipe(split(/\n/)).on('data', function cb (data) {
      cb(null, data);

to run test

npm test

to run jshint

npm run jshint

to run code style

npm run code-style

to run check code coverage

npm run check-coverage

to open the code coverage report

npm run open-coverage