Mule how to map Json values ​​from payload to stream variables

I have a JDBC COnnector that retrieves values ​​from a database and I mapped them to an object in JSON. Now I want to extract specific values ​​from json into stream variables. When I try log # [message.payload], I get the full payload in a log that is in JSON format. However, when I try to select an attribute (for example, testattribute in json) # [message.payload.testattribute], I get a mule expression error. How can I reference json values?

+4
source share
4 answers

Once the payload is a JSON string, you can no longer extract anything from it with an expression.

So either:

  • use MEL to extract values ​​into stream variables before converting the payload to JSON,
  • convert the JSON payload to POJO or Map / Lists, use MEL to extract the values ​​into stream variables, and convert the payload to JSON again.
+3
source

If your requirement is to get values ​​from json data, you can use a simple MEL expression like

#[json:<name of the node>] 

For example, if you have json example data like this

 { "token" : 123, "id" : 456, "email" : " abc@abc.com ", "status" : "Success" } 

and if you want to get the identifier from the data, just use a simple simple MEL.

 #[json:'id'] 
+3
source

Mule has a JSON-to-Object transformer that can be used to get JSON elements. For example, if your JSON is following: -

 { "token" : 123, "id" : 456, "email" : " abc@abc.com ", "status" : "Success" } 

Now, to extract the elements, you need to use: -

 <json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object" /> 

And then you can extract as: - #[message.payload.email] or #[message.payload.status]

+2
source

Use the following components

  • Object - String
  • Object - JSON

then u can directly retrieve the value using the json expression as # {json: email} , in the below xml I used the component variable to set # {json: email} in the stream variable "var1" for ease of use

Note: json expression is available even in studio version 5.x

  <object-to-string-transformer doc:name="Object to String"/> <json:object-to-json-transformer doc:name="Object to JSON"/> <set-variable variableName="var1" value="#[json:email]" doc:name="Variable"/> 
+1
source

Source: https://habr.com/ru/post/1501134/


All Articles