0.2.0 • Published 10 years ago
json-segment v0.2.0
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 = require('fs');
var json_segment = ('json_segment');
// create fs stream
var fstream = fs.createReadStream('./data/test.json');
// data:
// [{"ww":1},{"ww":2},{"ww":3},{"ww":4},{"ww":5},{"ww":6},{"ww":7},{"ww":8},{"ww":9},{"ww":10}]
var segment = json_segment(fstream, 2, cb);
function cb(err, result) {
if(err) {
throw Error(err);
}else {
// an json array seperate to 10 segments
console.log(result);
// 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 = require('json_segment');
var obj2= [{ww:1},{ww:2},{ww:3},{ww:4},{ww:5},{ww:6},{ww:7},{ww:8},{ww:9},{ww:10}];
json_segment(obj2, 2, function(err, result) {
if(err) {
throw Error(err);
}else {
console.log(result);
// result:
// [ [ { ww: 1 }, { ww: 2 }, { ww: 3 }, { ww: 4 }, { ww: 5 } ],
// [ { ww: 6 }, { ww: 7 }, { ww: 8 }, { ww: 9 }, { ww: 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 = require('fs');
var json_segment = require('json_segment');
var fstream2 = fs.createReadStream('./test/data/color.json');
var segment = json_segment(fstream2, 'color', cb);
function cb(err, result) {
if(err) {
throw Error(err);
}else {
// sort by color
console.log(result);
// 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 = require('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"
}
];
json_segment(color, 'color', function(err, result) {
if(err) {
throw Error(err);
}else {
console.log(result);
// 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' } ] ]
}
})
API
Streaming API
var fstream2 = fs.createReadStream('./test/data/color.json');
var segment = json_segment(fstream2, 'color', 'utf8', cb);
- first: fs stream
- second: number or string seperate rules,
string
will sort by key. - third: encodingoptional
- forth: callback function
Passing raw JSON API
json_segment(color, 'color', function(err, result) {
if(err) {
throw Error(err);
}else {
console.log(result);
}
})
- first: raw json
- second: number or string seperate rules,
string
will sort by key. - forth: callback function
License
MIT @chilijung