json-list-paths v1.0.0
JSON List Paths
A node.js utility to iterate through a JSON object and retrieve a list of all available paths. As well as property type and values.
Install
npm install json-list-paths
Usage
Pass a parsed JSON object to the function and will return a JSONPathList
object. See methods below for usefullness.
var jlp = require('json-list-paths')
var json = {
a: 'abc',
b: 123,
c: {},
d: []
}
jlp(json);
Methods
get (path)
Returns the JSONPathList
object matching that key.
list (options)
With no options the list method will return a string array of all the available paths in the object with a few notable identifiers. A []
in the path indicates that the previous key was an array and any elements afterwards were found in a child of that array. When using the reduce
method you will come across {}
in the keys indicated that previous object was collapsed.
Options
- types boolean, number *optional - Returns types each path was found to have. As well as including some added types like
number-int
,string-int
,number-decimal
,string-decimal
. That further add clarity to what type of values where found. Iftrue
or0
will return all types, a number will returnn
amount per path. - values boolean, number *optional - Returns primitive values each path was found to have. If
true
or0
will return all values, a number will returnn
amount per path. - children boolean, number *optional - Returns object properties if any for each path. If
true
or0
will return all values, a number will returnn
amount per path. - keys boolean, number *optional - Is like
children
but returns properties for collapsed paths. SeeReduce
. Iftrue
or0
will return all values, a number will returnn
amount per path.
reduce (options)
The reduce method is used to collapse object properties down to one. This is useful if you have a JSON object that uses an id for the key (an example might be an object with SKUs as the the keys). Objects that are collapsed will have {}
in place of where the keys would have been in the path.
Options
- keylimit number * optional - If an object has more keys than this number that it will be collapsed.
- match array * optional - This is an
array
ofstrings
orregular expressions
that will be used to evalute each path, to see if it's child properties should collapse. - replace array * optional - This is an array of objects that will rewrite a given key, if the parent path and key match the values supplied.
- path string, regular expression - The parent path to match.
- key string, regular expression - The key to match.
- replace string - The string to replace with.
- stop boolean - Stop all further replacements.
Examples
Using all available options for list
var jlp = require('json-list-paths');
var json = {
items: {
'PN1234': {
name: 'Item 1',
value: 12,
quantity: 12
},
'PN123452': {
name: 'Item 2',
value: 10,
quantity: 1
},
'G123': {
name: 'Item 3',
value: 20,
quantity: 4
}
},
total: {
items: 3,
quantity: 17,
value: 234
}
}
jlp(json)
.reduce({
match: ['.items']
})
.list({
keys: true,
values: true,
types: true,
children: true
});
// {
// '.': {
// types: ['object'],
// children: ['items', 'total']
// },
// '.items': {
// types: ['object'],
// children: ['{}']
// },
// '.items.{}':
// {
// types: ['dictionary', 'object'],
// keys: ['PN1234', 'PN123452', 'G123'],
// children: ['name', 'value', 'quantity']
// },
// '.items.{}.name':
// {
// values: ['Item 1', 'Item 2', 'Item 3'],
// types: ['string']
// },
// '.items.{}.value': {
// values: ['10', '12', '20'],
// types: ['number-int']
// },
// '.items.{}.quantity': {
// values: ['1', '4', '12'],
// types: ['number-int']
// },
// '.total':
// {
// types: ['object'],
// children: ['items', 'quantity', 'value']
// },
// '.total.items': {
// values: ['3'],
// types: ['number-int']
// },
// '.total.quantity': {
// values: ['17'],
// types: ['number-int']
// },
// '.total.value': {
// values: ['234'],
// types: ['number-int']
// }
// }
Using regular expressions reduce
replace option
var json = {
items: {
'PN1234': {
name: 'Item 1',
value: 12,
quantity: 12,
main: true
},
'PN123452': {
name: 'Item 2',
value: 10,
quantity: 1
},
'G123': {
name: 'Item 3',
value: 20,
quantity: 4,
style: 'g'
}
},
total: {
items: 3,
quantity: 17,
value: 234
}
}
jlp(json)
.reduce({
replace: [{
path: '.items',
key: /^([A-Z]+).*/,
replace: '$1_n',
stop: true
}]
})
.list();
// Result:
// [
// '.',
// '.items',
// '.items.PN_n',
// '.items.PN_n.name',
// '.items.PN_n.value',
// '.items.PN_n.quantity',
// '.items.PN_n.main',
// '.items.G_n',
// '.items.G_n.name',
// '.items.G_n.value',
// '.items.G_n.quantity',
// '.items.G_n.style',
// '.total',
// '.total.items',
// '.total.quantity',
// '.total.value'
// ]
7 years ago