This is an extension of a fork of the original JMESPath.
It fully complies with the spec, and includes all tests
from the original jmespath.js
project.
There is an article on the XML website claiming that You Should Be Using XSLT 3.0 to process JSON. If you take a look at the XSLT given for performing the transformation of the JSON in their example, it's (like XML in general) overly verbose.
I couldn't quite reproduce the same thing using the original JMESPath, but with my fork of JMESPath that allows for the
addition of custom functions, I could. By adding the toPairs
function, there is now an elegant solution to the example
from that XML article:
toPairs(employees)[*]
.merge(
[1],
{
id: [0],
fullName: join(' ', [[1].firstname, [1].surname]),
reverseName: join(', ', [[1].surname, [1].firstname])
}
)
The original jmespath.js repository has not been updated for many years. There are many unanswered issues, including this one inquiring about the project status. Also on the website repo there is an issue about the project status. I submitted a pull request.
In the meantime, I published my fork as a "scoped package" on NPM here.
This package is an extension of that fork, with some extra utility functions that I find useful. I added a few bits of lodash and some string functions. Open an issue if you need any extra functions added, or use the code in
src/main.js
as an example and extend @daz.is/jmespath.js
with your own functions.
Install from npm:
npm install --save jmespath-plus
https://codesandbox.io/embed/focused-lichterman-y867l?fontsize=14&hidenavigation=1&theme=dark
const jmespath = require('jmespath-plus');
const v = jmespath.search({ a: 42 }, 'a');
console.log(v); // output = 42
- additional functions have been added (see below)
- more custom functions can be added by you
- the
$
character can be used within expressions to reference the root - parsed expressions can be cached and run against multiple data
The following extra functions have been added:
Converts string to lowercase.
Removes any spaces from beginning and end of string.
Split string by separator.
Convert an object into an array of key value pairs.
Convert an array of key value pairs back into an object.
Return the object but omitting the items with the specified keys.
Return a new object with only the items with the specified keys.