json-segment
Single json file into segments (json format).
Install
npm install json-segment
Usage
Seperate by number
If the second argument is a number, this will seperate the files into the number given.
Streaming file systems.
var fs = ;var json_segment = 'json_segment';// create fs streamvar fstream = fs;// data:// [{"ww":1},{"ww":2},{"ww":3},{"ww":4},{"ww":5},{"ww":6},{"ww":7},{"ww":8},{"ww":9},{"ww":10}]var segment = ; { iferr throw Errorerr; else // an json array seperate to 10 segments console; // result: // [ [ { ww: 1 }, { ww: 2 }, { ww: 3 }, { ww: 4 }, { ww: 5 } ], // [ { ww: 6 }, { ww: 7 }, { ww: 8 }, { ww: 9 }, { ww: 10 } ] ] }
or
Passing Js object
var json_segment = ; var obj2= ww:1ww:2ww:3ww:4ww:5ww:6ww:7ww:8ww:9ww:10;
Seperate by key-value
If the second argument is a string, it is defined to seperate the data by using the key-value, the same value in the same key, will seperate into the same json array in the output.
Streaming file system
var fs = ;var json_segment = ;var fstream2 = fs;var segment = ; { iferr throw Errorerr; else // sort by color console; // result: // [ [ { color: 'red', value: '#f00' }, // { color: 'red', value: '#f00' }, // { color: 'red', value: '#f00' } ], // [ { color: 'green', value: '#0f0' }, // { color: 'green', value: '#0f0' }, // { color: 'green', value: '#0f0' } ], // [ { color: 'blue', value: '#00f' }, // { color: 'blue', value: '#00f' }, // { color: 'blue', value: '#00f' } ], // [ { color: 'cyan', value: '#0ff' }, // { color: 'cyan', value: '#0ff' } ], // [ { color: 'magenta', value: '#f0f' }, // { color: 'magenta', value: '#f0f' } ], // [ { color: 'yellow', value: '#ff0' }, // { color: 'yellow', value: '#ff0' } ], // [ { color: 'black', value: '#000' }, // { color: 'black', value: '#000' } ] ] }
or
Passing Js object
var json_segment = ; var color = "color": "red" "value": "#f00" "color": "green" "value": "#0f0" "color": "blue" "value": "#00f" "color": "cyan" "value": "#0ff" "color": "magenta" "value": "#f0f" "color": "yellow" "value": "#ff0" "color": "black" "value": "#000" "color": "red" "value": "#f00" "color": "green" "value": "#0f0" "color": "blue" "value": "#00f" "color": "cyan" "value": "#0ff" "color": "magenta" "value": "#f0f" "color": "yellow" "value": "#ff0" "color": "black" "value": "#000" "color": "red" "value": "#f00" "color": "green" "value": "#0f0" "color": "blue" "value": "#00f" ;
API
Streaming API
var fstream2 = fs;var segment = ;
- first: fs stream
- second: [number or string] seperate rules,
string
will sort by key. - third: encoding[optional]
- forth: callback function
Passing raw JSON API
- first: raw json
- second: [number or string] seperate rules,
string
will sort by key. - forth: callback function
License
MIT @chilijung