Comments (7)
The design of JSONata is to build the expressions once and evaluate the same expression against different data, for example:
const expr = jsonata('foo.bar')
expr.evaluate(data1)
expr.evaluate(data2)
expr.evaluate(data3)
This is because parsing the expression can be computationally costly, and you might want to cache built expressions.
As I understand it, your case is the reverse: you want to evaluate different expressions against the same data. One way to achieve this would be to simply make your expression more complete, so instead of:
const result1 = jsonata('foo.bar').evaluate(data)
const result2 = jsonata('foo.baz').evaluate(data)
const result3 = jsonata('bax.bar').evaluate(data)
You could have an expression that evaluates all three at the same time, such as:
// Must be a string
const expression = `{
"part1": foo.bar,
"part2": foo.baz,
"part3": bax.bar
}`
const result = jsonata(expression).evaluate(data)
const result1 = result.part1
const result2 = result.part2
const result3 = result.part3
Check out an example: http://try.jsonata.org/ryuVA5PU-
How much does this help with your use case?
from jsonata.
Hi! I would first like to say thank you for your response. In terms of our case, we actually implement something similar to the solution you posted, but this is in a different implementation than the one I am working on. In terms of the one I am working on I believe your solution may actually work, as currrently I essentially have to evaluate expressions at certain repeating points in a giant JSON document. I currenlty do this by calling jsonata one at a time at each point. I will now try your implementation by collecting these expressions and evaluating them at once. Thanks for your help!
from jsonata.
@blgm actually as I work though this for my specific project/task it seems your approach not might work in my case...The issue I have is that I have a giant JSON that I need to call jsonata on as I traverse down the JSON I need to contantly call jsonata and use the same input file for each call. I was wondering if there was any way I could just load my respective JSON file (the one I put as the params for the evaluate function) and then freely call jsonata function with different expressions without having to constantly call the same file for the evaluate params. If further clarification is required, I am happy to provide. Thank you!
from jsonata.
@vtec11001, as JSONata requires all the JSON to have been parsed into a JavaScript object, there is no computing efficiency to be gained by calling evaluate()
only once. However this touches on the limitation that JSONata acts on JavaScript objects, and therefore cannot be used on a JSON structure that is too large to be held in memory. See #45 for a discussion on that. If your giant JSON can be be held in a JavaScript object then calling evaluate()
multiple times should work reasonably efficiently. If the JSON object is too large to be held in memory, then you could consider combining it with a partial JSON parser such as JSONStream which will break the JSON object into a series of smaller JavaScript objects which JSONata can act on. We don't have any experience of doing that, but are keen to understand the use cases for handling large JSON objects and JSON streams.
from jsonata.
@vtec11001, another idea. If you are looking to apply lots of expressions to a single JSON input, you could combine all of your expressions into a single object containing name/expression pairs and evaluate that once. For example, if you have the following 3 expressions:
Account.Order[0].OrderID
--> "order103"
$sum(Account.Order.Product.(Price * Quantity))
--> 336.36
$count(Account.Order.Product)
--> 4
you could combine them and evaluate the following once:
{
'expr1': Account.Order[0].OrderID,
'expr2': $sum(Account.Order.Product.(Price * Quantity)),
'expr3': $count(Account.Order.Product)
}
to produce the combined result:
{
"expr1": "order103",
"expr2": 336.36,
"expr3": 4
}
See this example in the exerciser: http://try.jsonata.org/ByVyuGl9Z
from jsonata.
@andrew-coleman thank you for mentioning this implementation. I wasn't aware that the expressions could be contained in json itself.
from jsonata.
Closing this issue, as I think it is now resolved. Feel free to re-open if you have any follow on questions.
from jsonata.
Related Issues (20)
- ($.some_non_existent_key != 'anything') evaluates to false HOT 3
- $string(number) capped at precision-15
- Error handling and asynchronous behavior in evaluate function
- Rounding number without "Round half to even"
- There isn't a uuid() function for JSONata HOT 2
- Hard spaces in documentation not rendered correctly
- Security contact for JSONata HOT 4
- default query on try.jsonata.org HOT 1
- is 2.0.4 a fake release? HOT 4
- Multi-level JSON processing: Unwrapping Objects
- How to work with parent object and transform values
- Stronger `registerFunction` TS types by inferring types from signature, and a road to a language service (PR coming!)
- Feature: Jsonata json type inference HOT 5
- JSONata leaks internal references in expression when using lambdas
- JSONAta expression can be created that ends up in mutation of original input object
- `__evaluate_entry` and `__evaluate_exit` can be overridden inside expression
- $sort fails for >6000-element arrays HOT 1
- Why is jsonata async/await? HOT 2
- Unexpected array access behaviour HOT 1
- Missing Type in Interface ExprNode, lhs can be both an ExprNode and ExprNode[]
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonata.