@mangos/jxpath v1.0.13
JXPath
JXPath is a query langauge for JavaScript Objects (parsed JSON).
Enhancements over jsonpath include:
- parent operator ../
- query using regular expressions (on both property names and their values)
- recursive descent operator **
- lazy tree walking
npm install @mangos/jxpathQuery operators overview
| operator | jxpath | example | 
|---|---|---|
| literal_text | exact selector | /persons/adress/city | 
| .. | parent selector | /persons/adress/[zip=/$FL/]/../firstName | 
| ** | recursive descent | [/employees/**/[name=Clark Kent]/address | 
| [key=value] | predicate | [city=London],[city=/town$/],[/name$/=/^Smith/] | 
| [regexp1=regexp2] | regexp predicate (including regexp flags) | [city=/town$/igm],[/name$/=/^Smith/],[/name$/=Mr Dubois] | 
Note: more operators will be implemented, create an issue if you have an idea for a novice operator
Query "path" elements are seperated by / token and predicates are enclosed between /[  ]/ tokens.
JXPath alaws returns a array of values/objects, if nothing was selected by the query the array will be empty.
JXPath navigates through arrays and objects agnosticly.
Look the the following JS object:
const data = {
    manager: {
        firstName: 'Big',
        lastName: 'Boss'
    }
    employees: [ 
        {
            firstName: 'Tammy',
            lastName: 'Brant',
            address: {
                zip: 'AL 36104'
                city: 'Calumet City',
                street: '837 West St.'
            }
        },
        {
            firstName: 'Roy',
            lastName: 'White',
            address: {
                zip: 'AL 36487',
                city: 'Tullahoma',
                street: '843 Golden Star Avenue'
            }
        },
        {
            firstName: 'James',
            lastName: 'Kirk',
            address: {
                zip: 'FL 32301',
                city: 'Jackson Heights',
                street: '572 Myrtle Avenue'
            }
        }
    ]
};
const  path =  // see examples below
const jxpath = require('@mangos/jxpath');
const iterator = jxpath( path , data); // returns iterator, lazy lexing
console.log( Array.from( iterator );
//-> result , see belowThe /employees/ is an array of objects, and /manager is single nested object, JXPath query treats them both agnosticly.
Predicate [..] query selector
Predicates have the general pattern /[key=value]/; both key and value can be regular expressions
regular expression predicates
The regular expresion predicate will take flags igmsuy after the last regexp delimiter /.
- A path of /employees/[firstName=/(Tammy|Roy)/]/lastNamewould return the the lastNames:[ 'Brant', 'White' ]omittingKirk.
- A path of /employees/[/Name$/=/.*/]/firstNamewould return the first-and lastNames combined:[ 'Tammy', 'Brant' , 'Roy' , 'White' , 'James' , 'Kirk' ]
- A path of /manager/[/Name$/i=/^B/i](not the use of theiflag) will return the object valuemanagersince bothfirstNameandlastNamematch the left side expression and both values start with the upperCaseB(or lowerCasebbecause of the use of flagi).
literal predicates
- A path of /manager/firstNamereturns the result[ 'Big' ].
- A path of /employees/firstNamereturns the result[ 'Tammy', 'Roy', 'James' ].
- A path of /employees/non-existant-pathreturns an empty result[].
Parent selector
A parent selector is the two dots .. as it is in XPath.
- A path of /employees/address/[zip=/^AL/]/../firstNamewill give back the result[ 'Tammy', 'Roy', ], aka all first names of employees having a zipcode starting withAL.
Recursive descent selector
A recursive descent selector is the ** as it is in XPath.
- A path of /**/zipwill give back the zip prop values in the JS object (descending through objects or array of objects). Result['AL 36104','AL 36487', 'FL 32301'].
Functional
Can use jxpath in a curried fashion
const  path =  // see examples 
const jxpath = require('@mangos/jxpath');
const curry = jxpath( path ); // curried 
const iterator = curry(data); // process data, re-use the curried versionIgnore property names
Can use jxpath can ignore properties (especially handy if the object has circular references).
- The third argument in the jxpathis a singular field containing the name of the property to ignore (recursivly found in objects).
- The second argument in the curried form of jxpathis a singular field containing the name of the property to ignore (recursivly found in objects).
const  path =  '**/[city=New York]/../firstName';
const jxpath = require('@mangos/jxpath');
// curried form
const curry = jxpath( path ); // curried 
const iterator = curry(data, 'parent' ); // ignore "parent" property when doing recursive descent
// non curried form
const iterator = jxpath( path, data, 'parent' ); // ignore "parent" property when doing recursive descentSupport the work by starring this repo on github. _Part of the mangos monorepo of data wrangling tools.
Feedback
We appreceate any feedback, with new ideas, to enhance this tool suite. File an issue here
Before contributing, please read our contributing guidelines and code of conduct.
License
Copyright (c) 2019-2020 Jacob Bogers info@mail.jacob-bogers.com.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.