Git Product home page Git Product logo

Comments (9)

wnm3 avatar wnm3 commented on July 16, 2024

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.

wnm3 avatar wnm3 commented on July 16, 2024

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.

cray74 avatar cray74 commented on July 16, 2024

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.

wnm3 avatar wnm3 commented on July 16, 2024

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.

wnm3 avatar wnm3 commented on July 16, 2024

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.

cray74 avatar cray74 commented on July 16, 2024

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.

shrickus avatar shrickus commented on July 16, 2024

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.

andrew-coleman avatar andrew-coleman commented on July 16, 2024

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.

andrew-coleman avatar andrew-coleman commented on July 16, 2024

I believe this is resolved as per last comment. Please re-open if not.

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.