@mojule/json-dom-plugins
v0.1.5
Published
Plugins for @mojule/json-tree that let you treat JSON like a DOM
Downloads
10
Readme
json-dom-plugins
Plugins for json-tree that let you treat JSON like a DOM. Built on dom-plugins.
Wait, what?
It's more useful than you might think - using query selectors to find things in an object graph for example:
{
"name": "backpack",
"weight": 1,
"contents": [
{
"name": "apple",
"weight": 0.85
}
]
}
const tree = JsonTree( json )
const weightNodes = tree.querySelectorAll( 'number.weight' )
const totalWeight = weightNodes.reduce(
( sum, node ) => sum + node.nodeValue(),
0
)
console.log( totalWeight ) // 1.85
Install
npm install @mojule/json-dom-plugins
Example
const { Factory } = require( '@mojule/json-tree' )
const domPlugins = require( '@mojule/json-dom-plugins' )
const json = require( './path/to/my.json' )
const Tree = Factory( domPlugins )
const tree = Tree( json )
const weightNodes = tree.querySelectorAll( 'number.weight' )
// etc