Accessing Payload, Variables and Environment Properties in Kumologica
This article will demonstrate how to access or manipulate message payload, variables, and environment variables inside Kumologica flow.
Join the DZone community and get the full member experience.
Join For FreeA Kumologica API or service integration can be represented as a connected network of nodes, each node is responsible to take a message (request) and produce zero or many outputs, which in turn becomes the input to the next node. A Kumologica flow uses only 3 elements for the propagation and manipulation of data when moving from one node to the other.
1. Message — msg
2. Variables — vars
3. Environment — env
Message
Message (msg
) object is a piece of information that node use to communicate with each other. Nodes work on the object their predefined logic and modify the object. The basic structure of the message object is as follows.
- _msgid: Unique execution identifier created for each execution.
- error: This property will hold the error being caught by the Error Node.
- header: This property is an object hold metadata added by a node. Information contained in this object should not be modified by other nodes.
- payload: This property is an object hold information added by the node. Unlike the header property, this property is meant to be mutable, and it has a special meaning for some nodes.
How to Access Message Object?
Message object can be accessed in all the nodes with the expression msg
. You may access the payload inside the message using the expression msg.payload
. Based on the object type of within the payload the content can be further traversed down. Eg: Let's assume that a payload is a JSON object with the following structure.
x
{
"details" : {
"name" : "kumo",
"location" : "Kerala"
},
"Country" : "India"
}
In the above JSON payload, the location field can be accessed in any nodes using the following expression.
msg.payload.details.location
Now to set a value to the location filed in the same JSON payload can be done using different ways like using Set-Property node, Datamapper node, or a Function node. Inside the function node, you can set the value to the above-shown JSON payload as given below.
msg.payload.details.location = 'Delhi'
Or if you want to introduce a new field in the existing payload you can do as follows.
msg.payload.direction = "South"
This will result in the below-given payload output.
xxxxxxxxxx
{
"details" : {
"name" : "kumo",
"location" : "Kerala"
},
"Country" : "India" ,
"Direction" : "South"
}
Similarly, you can introduce a new field directly on the msg object structure as well.
msg.mydata = "testdata"
Variables
Variables are temporary storage which can be used when you want to preserve certain content like payload which can be further used at any point in the flow. Variables like message object can be accessed using the expression vars
. You can set a variable either using a Set-Property node or a Function node.
How to Access Variable Object?
Inside the Set-Property node, you need to select $vars
from the drop-down to create and initialize a variable.
Then provide the variable name. On the to section provide the value you want to set on the variable. For example, in the below Set-Property, I am setting payload inside the variable with the name "mytemp".
You can access the variable to retrieve the content similar to the message object. Eg. Assume that the JSON payload mentioned in the earlier example in the message section is stored in the variable mytemp using the Set-Property node as shown above. We can access the location field in the JSON structure using the below-given expression.
vars.details.location
Environment
Environment variable unlike the regular variable is set from outside the context of Kumologica flow. These are variables injected as part of the environment properties of your lambda application.
How to Access Environment Object?
Environment variables can be accessed using the expression env. The property can be accessed in any node using the following expression. Eg. I have a database hostname that is set as part of the environment and I need to access it inside the flow.
env.db.hostname
Summary
Know the above 3 elements of Kumologica flow you can manipulate data whichever way you like to build your cool application. All the expressions covered in this article are fundamentally JSONata expressions which means that you can apply the principles of JSONata for the manipulation of your data.
Opinions expressed by DZone contributors are their own.
Comments