Skip to content

Conversation

@movitto
Copy link

@movitto movitto commented Jun 21, 2018

Hey thanks for the awesome library. I'm sending a PR adding functionality where variables and functions can now be injected into the expression scope like so:

jsonpath.scope({foo:'bar',parseInt:parseInt})

This is so the client can inject custom variables and functions to be used when evaluating expressions like so:

$..[?(parseInt(@.Amount) > 0)]

Variables and funcations can now be injected into the expression
scope like so:

  jsonpath.scope({foo:'bar',parseInt:parseInt})

Thus the client can incorporate builtin or custom methods in
jsonpath expressions:

  $..[?(parseInt(@.Amount) > 0)]
@sdawood
Copy link
Contributor

sdawood commented Sep 21, 2018

I'd say jsonpath should focus on query, safely and as fast as possible.
Check out 'json-tots' on npm for transformations capabilities.
Disclaimer:
This is my own view, I'm not speaking for the maintainer(s) of this package

@movitto
Copy link
Author

movitto commented Sep 21, 2018

@sdawood appreciate the comments. This patch wouldn't preclude fast / safe querying, nor does it serve as a transformation mechanism, rather it allows the developer to use custom helper / utility methods during the query process.

The primary use case we had when developing this patch was to query JSON transactions as returned from rippled (https://github.com/ripple/rippled) which stores all floating point values as strings (so as to avoid platform-specific discrepancies with floating point representation). This patch would allow the developer to query this json for float values which match specified numerical conditions. Otherwise the dev would need to run a first pass using jsonpath, then do a second round of querying using some other custom logic.

@sdawood
Copy link
Contributor

sdawood commented Sep 22, 2018

See, our discussion and comparing problem scopes, one-query -> jsonpath language is specified.
custom systematic transformation -> is where I started experimenting with json-tots, and focus on trasducer-pipelines for values cleanup and casting.
And I use jsonpath as my assembly language to access json shaped memory faster and without cognitive overhead of custom assertions.

@movitto, Great problem statement and enough intuition to start a good conversation amongst the json consumer community 😎

@infojunkie
Copy link

infojunkie commented Sep 23, 2018

Thanks for this PR. With it, I was able to make partial matches easily:

jp.scope({ normalizeCompare: (a,b) => {
  return a.toLowerCase().includes(b.toLowerCase());
}});
jp.nodes(books, `$..sheets[?(normalizeCompare(@.title, '${query}'))]`)

https://github.com/infojunkie/sheetdex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants