0

In Mule4, I need to convert json sample data in to dynamic XML format, I have tried with dataweave(2.0) field mapping, getting null values. Does anyone can help me on this?

Alex Sergeenko
  • 642
  • 5
  • 22
Saikumar R
  • 9
  • 1
  • 3

1 Answers1

1

If the question is just asking out to build up XML output from JSON input, that is a pretty open-ended question. What do you want to evaluate dynamically? You could, for example, use part of the payload to set values in the DataWeave expression.

There is a more difficult version of this question: how to dynamically evaluate DataWeave code constructed into an input string, where this string could be read from various script files, or even constructed in-line from some input data (payload, attributes, or variables).

Here is another example covered in our MuleSoft DataWeave training course at http://training.mulesoft.com.

You can use a Dynamic Evaluate component to dynamically evaluate a constructed DataWeave expression string. Here is an example that replaces the uName parameter with a dynamic value.

enter image description here

Also, the expression is configured to read in different script files based on some condition:

output application/json 
--- 
do {
    var choice = attributes.queryParams.script default "NO_SCRIPT"
    ---
    if(choice == "NO_SCRIPT") 
    "output application/json --- {result: 'NO SCRIPT ENTERED'}"
    else if(choice == "script1") vars.script1
    else if (choice == "script2")  vars.script2
    else read(choice)
}

Here are two example scripts that substitutes values for uName and produce different types of output (XML vs. JSON).

This is script1:

output application/xml 
--- 
root: { message: "order " 
++ attributes.queryParams.orderid 
++ " has been received from " 
++ uName, items: payload.items} 

This is script2:

output application/json 
--- 
root: {  message: "Order2 " 
++ attributes.queryParams.orderid 
++ " has been received from " 
++ uName, items: payload.items}

Notice that this example is dangerous. It lets the web client inject any DataWeave code into the Mule flow, so this example should never be copied into production code, but it does demonstrate the ability to run any DataWeave code passed into a Mule application.

Ethan Port
  • 166
  • 8