Comments (8)
The function you are looking for is $lookup(object, key)
For your sample data input, this expression returns what you want:
$keys().{
"name": $lookup($$, $).name,
"key": $
}
Alternately, you can use the $spread(object)
function to create separate object from each field of the original object. Then to get the original field string, use $keys(object)
and grab the first element of the 1-element array of keys:
$spread().{
"name": *.name,
"key": $keys()[0]
}
This is another one of those cases where the expression would be greatly simplified if only there was a way to refer to the parent (or more generally, any ancestor) object. I think the problem is that the lambda closures don't have a way to access the object/function that called them -- Andrew may be able to explain again why this is not possible.
from jsonata.
Yeah, I sympathise with the desire to access the 'parent'. It's so easy to do in XPath, so why not JSONata? Let me try and explain:
When processing XML data, XPath is working against a DOM tree which is a fully interconnected set of nodes
, each of which has direct access to first child
, last child
, previous sibling
, next sibling
and parent
. Also, each node has a name
and a value
, so even if you navigated to a node using a wildcard, you still have access to its name.
JSON is a much simpler data structure. There is no concept of nodes
; a value
itself doesn't have a name
even though an object is a set of name/value pairs; nor is there any connection between values. Sure, I could have designed the language to 'parse' the input JSON into the equivalent of a DOM tree, but that would have made the language and its implementation far more complex, and I was striving for simplicity.
I've spent a lot of time thinking of how to simplify the sort of transformation that you are trying to do here, and I'll continue to scratch my head on this.
from jsonata.
A while ago I worked with an xpath like parser called bpath in SAP ABAP.
There existed the concept "framework access class". This class was called every time the parser moved up or down the path and by using this intelligently the parent could be attached to the current object and later retrieved. I experimented with this concept in JS and this concept is quite trivial.
The added benefit is that also non JSON data, such as db data can be used simply by swapping the framework object
So if anyone is interested I could elaborate somewhere.
Cheers
Martin
from jsonata.
@Allam76 I'm interested
from jsonata.
OK, I'm travelling today, hopefully I can clean and present a small version tomorrow.
from jsonata.
I put the framework access class from the other project on my fork framework-access
This solution has a parser just like jsonata, I did not publish that code since it is very ugly. The parser parses a syntax similar to xpath. Every time the parser determines that something moves on the path, a call is delegated to the framework access class. This class has three types of methods:
- Everything that ends with name, these are for code completion.
- All others, they are used to set a current object, manipulating collections, parent, children etc.
- Instantiation methods, useless in a js context.
These methods in 2) are similar to the evaluate* functions in jsonata.
To translate this to jsonata, one would need to break out the evaluate* functions in a separate file, add them to the parser and allow them to be overridden. Inside one would also need to capture the current object, depending on where the parser is in the path at that moment. Maybe that could be realized with a $current variable and/or $parent variable?
Lastly one must with this approach accept side effects, typically only to set the parent. Please also note that this implementation comes from a class based language, much less flexible than js. Therefore the code is sub optimal for our use case. We can without doubt figure something out that is much more clever for our scenario.
from jsonata.
I'm super busy at the moment but when I have time I could try to do the following:
- Put evaluate* in a seperate file to allow to be overridden.
- Add $parent and $current variables.
This could potentially solve this issue and the mixin issue at the same time.
With this, we have the benefit to use jsonata to transform a lot more than json. I personally use it with postgresql relational DB.
from jsonata.
Closing, since discussion has ceased.
from jsonata.
Related Issues (20)
- How to return the value from jsonata instead of promise/async HOT 3
- Dry run, return expected schema for a given input schema
- How to handle undefined value HOT 2
- $formatDateTime(millis, picture [, timezone]) and $parseDateTime(timestamp, picture) HOT 1
- Clarification : Is there Any Feature for Apply Break or Continue in Looping HOT 1
- parallel map
- switch case or if else
- Truthiness of numbers is completely random
- Help resolve current pinning to 1.8.6 for JSONata-Wrapper (possibly ducktape compatibility)
- Reserved Tokens Documentation
- ($.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
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.