json-access
v1.0.8
Published
json数据快速访问
Downloads
3
Readme
json-access
access json with path(split by '/')
Install
npm i json-access -S
Usage
const jsonAccess = require('json-access');
let a = {
level1: 'name',
level2: 100,
level3: [
1,
2,
3,
4,
5,
],
level4: [{
name: 'sub',
}],
'level5': {
name: 'test',
objs: [{
name: 'sub'
}]
}
};
console.log('level1:', jsonSelect.getEx(a, 'level1'));
console.log('level3/[2]:', jsonSelect.getEx(a, 'level3/[2]'));
console.log('level4/sub:', jsonSelect.getEx(a, 'level4/sub'));
console.log('level4/[0]/name:', jsonSelect.getEx(a, 'level4/[0]/name'));
console.log('level4/sub/[0]/name:', jsonSelect.getEx(a, 'level4/sub/[0]/name'));
console.log('level5/objs/[0]:', jsonSelect.getEx(a, 'level5/objs/[0]'));
console.log('level5/objs/[0]/name:', jsonSelect.getEx(a, 'level5/objs/[0]/name'));
console.log('delete level1:', jsonSelect.deleteEx(a, 'level1'));
console.log('delete level3/[2]:', jsonSelect.deleteEx(a, 'level3/[2]'));
console.log('delete level5/objs/[0]/name:', jsonSelect.deleteEx(a, 'level5/objs/[0]/name'));
function desc:
|name|desc| |-| |get| throw error when path not exist or bad format| |getEx(input, path, def)|return def when error| |delete| return this, throw error when bad format| |deleteEx| return this|
path desc:
path item split by '/' exp: level3/[2] level3: json map [2]: json array index 2
Example
See example.
License
The MIT License