Comments (9)
I see, I forgot that the transform part is in the context of the target so I needed to do:
msg~>|payload|{"originalInput":(input)}|
from jsonata.
How would I reference some other part of the json in the transform clause?
For example, if I have:
{
"msg":{
"payload":{
"entity":"junk",
"context":{
"display_type":{}
}
}
}
}
and I wanted to set the msg.payload.context.display_type to the value of msg.payload.entity
I am trying:
msg~>|payload.context.display_type|(msg.payload.entity)|
and even though (msg.payload.entity) resolves to "junk" the display_type remains {}
I think I need a way to reference anything in the transform clause but am not sure how to do this.
from jsonata.
Try addin "$$" in front of msg.payload.entity (to get to the root context):
(
$ ~> |msg.payload.context|{"display_type": $$.msg.payload.entity}|;
)
See http://try.jsonata.org/H1-5P8Dkf
from jsonata.
Based on a solution from here:
#70 (comment)
It seems by creating a variable, once can then reference that variable in the transform clause.
However, I think the expectation is the transform clause is an object, not a simple string or number. The following does work, but I'd prefer not to have to assign the variable:
(
$entityVal := {"display_type":(msg.payload.entity)};
msg~>|payload.context|$entityVal|
)
For example, this does not work:
msg~>|payload.context|{"display_type":(msg.payload.entity)}|
from jsonata.
Thanks @cray74 -- that works if I create the object in the clause.
Is there any way to allow the transform clause to evaluate to a simple type (string, int...) and still allow assignment to the transform target? or must we always reference an object?
from jsonata.
try wrapping your target in $string('your string'), or $number(9999) or any other function (actually returning an object, if I'm not mistaken).
I'm a total noob btw, so I might be totally off (but I've spent a few hours last weekend with very similar challenges, see http://try.jsonata.org/SkrU3nByf).
from jsonata.
According to the new documentation, the update expression (your "display_type") must evaluate to an object -- and it is evaluated relative to the location object or array (your "payload.context"). Since there is no way to reference parent objects, the "entity" expression needs the "$$." prefix, to be referenced starting at the root of the incoming data.
So this expression seems to work for me:
msg~>| payload.context | { "display_type": $$.msg.payload.entity } |
which in human-readable form is
return a transformed "msg" object, updating the "payload.context" object to have a "display_type" property value copied from the "msg.payload.entity" field
I don't understand why, but it's clear that Andrew does not expect the update expression to return a simple type (string, number, boolean). I can imagine that by requiring an object to be returned, the $merge() function logic can be used on the results. But if the update returns a string, why not just replace the location's value instead of trying to merge it?
I think this syntax describes the intent of the change more precisely, and in fewer characters:
msg~>| payload.context.display_type | $$.msg.payload.entity |
Unfortunately, using the branch 1.4 implementation, this syntax returns the error:
The insert/update clause of the transform expression must evaluate to an object: "junk"
from jsonata.
The reason this only works at the object level is because it needs a reference to the thing it is going to update. Take the following example:
{
"msg": {
"payload": {
"originalInput":"oldValue",
"input": {
"text": "get weather for jfk"
}
}
}
}
If the expression was:
msg ~> | payload.originalInput | 'newValue' |
the processor would evaluate the expression payload.originalInput
and get the string "oldValue"
. Not a pointer to that string; just the string value. It will have lost the reference to the object that contained that value. So we need to specify that the object we want to modify is payload
, and the updates that we want to make to that object is specified by the next part of the expression. I can't think of a way round this at the moment. It's a similar situation to the discussion, and my response in issue #83.
from jsonata.
I believe this is resolved as per last comment. Please re-open if not.
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.