# split-json

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

Latest version **2.2.0** (published 2015-12-18) · ISC license · 0 weekly downloads

## Install

```sh
npm install split-json
pnpm add split-json
yarn add split-json
bun add split-json
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.2.0 |
| Published | 2015-12-18 |
| First published | 2014-01-03 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.10 |
| Dependencies | 4 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | @joaquimserafim |
| Maintainers | quim |
| Keywords | split, stream, JSON |

## Links

- npm: https://www.npmjs.com/package/split-json
- Repository: https://github.com/joaquimserafim/split-json
- Issues: https://github.com/joaquimserafim/split-json/issues
- npm.io page: https://npm.io/package/split-json

## Dependencies (4)

- [through2](https://npm.io/package/through2.md) ^0.6.5
- [is-regexp](https://npm.io/package/is-regexp.md) ^1.0.0
- [is-js-object](https://npm.io/package/is-js-object.md) ^1.2.2
- [json-parse-safe](https://npm.io/package/json-parse-safe.md) ^1.0.3

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 2.2.0 (latest) — 2015-12-18
- 2.1.0 — 2015-07-21
- 2.0.0 — 2015-03-20
- 1.7.5 — 2014-10-23
- 1.6.5 — 2014-09-27
- 1.6.4 — 2014-09-26
- 1.6.3 — 2014-09-26
- 1.5.3 — 2014-07-30
- 1.5.2 — 2014-02-19
- 1.4.2 — 2014-02-08
- 1.3.2 — 2014-01-15
- 1.2.2 — 2014-01-14
- 1.2.1 — 2014-01-12
- 1.2.0 — 2014-01-12
- 1.1.0 — 2014-01-11
- … 1 more at https://npm.io/package/split-json/versions

## README

# split-json

<a href="https://nodei.co/npm/split-json/"><img src="https://nodei.co/npm/split-json.png?downloads=true"></a>

[![Build Status](https://travis-ci.org/joaquimserafim/split-json.png?branch=master)](https://travis-ci.org/joaquimserafim/split-json)


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


## API

```js

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

// js objects
stream.pipe(split.obj())
.on('data', function (obj) {
    console.log(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$)/*)

#### emitters
***on('data', callback(data))***
***on('end', [callback])***

___

##### split#JSONValid
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


##### split#isObject

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



## Examples

```js

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')

collection.find({})
.pipe(split.obj())
.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(/\n/)).on('data', function cb (data) {
      cb(null, data);
    })
})

```


## Development

##### this projet has been set up with a precommit that forces you to follow a code style, no jshint issues and 100% of code coverage before commit



to run test
``` js
npm test
```

to run jshint
``` js
npm run jshint
```

to run code style
``` js
npm run code-style
```

to run check code coverage
``` js
npm run check-coverage
```

to open the code coverage report
``` js
npm run open-coverage
```

---
_Source: https://npm.io/package/split-json · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
