Giter Club home page Giter Club logo

Comments (7)

blgm avatar blgm commented on August 22, 2024

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.

vtec11001 avatar vtec11001 commented on August 22, 2024

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.

vtec11001 avatar vtec11001 commented on August 22, 2024

@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.

blgm avatar blgm commented on August 22, 2024

@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.

andrew-coleman avatar andrew-coleman commented on August 22, 2024

@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.

vtec11001 avatar vtec11001 commented on August 22, 2024

@andrew-coleman thank you for mentioning this implementation. I wasn't aware that the expressions could be contained in json itself.

from jsonata.

andrew-coleman avatar andrew-coleman commented on August 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.