json-scrape
Version:
scrape json from messy input streams
74 lines (48 loc) • 1.1 kB
Markdown
Scrape json from messy input streams.
[](http://travis-ci.org/substack/json-scrape)
for a file with some json objects in it:
```
blip: { "type" : "test", "value" : 5 }
beep { "x" : 5, "y" : [5,6
,7,[8,9,10]] } boop
[]
bloorp
letters: ["a",
"b","c",
"d","e"] fgh
```
this script:
``` js
var createScraper = require('json-scrape');
var scraper = createScraper();
scraper.on('data', function (obj) {
console.dir(obj);
});
var fs = require('fs');
var s = fs.createReadStream(__dirname + '/data.txt');
s.pipe(scraper);
```
will pull out the json objects:
```
{ type: 'test', value: 5 }
{ x: 5, y: [ 5, 6, 7, [ 8, 9, 10 ] ] }
[ 1, 2, 3 ]
[ 'a', 'b', 'c', 'd', 'e' ]
```
Hooray!
``` js
var createScraper = require('json-scrape')
```
Return a through stream that accepts messy data with embedded json and emits
the objects in `'data'` events.
With [npm](http://npmjs.org) do:
```
npm install json-scrape
```
MIT