简体   繁体   中英

Mule Dataweave: How to dynamically map HTTP response(JSON) to XML output

I am building an application, where I have to hit a restful interface, and pass a specific section of the response to the UI.

The response model is quite huge with a lot of fields(String, array of objects, object,number datatypes), so using manual transformation is a big pain.

Is there a way to map the section of the response to a variable and directly send it to the UI.

I tried,

 %dw 2.0
%output application/xml

%var UserAcct= payload.UserResponse.UserDetailsResp.UserAccounts

---
{
  User: {
     "UserAccount": {   
        Account:  UserAcct
     }
   }
}

This doesn't work because, the payload has List, Array of Objects etc in the response and mapping to a variable throws an error. Is it possible to send the section payload.UserResponse.UserDetailsResp.UserAccounts directly in dataweave?? Please help.

It will be more helpful if you add Input payload, error and XML output.

Following is the code just by assuming your scenario. You can give this a try:

 %dw 2.0
 output application/xml
 ---
 {
   User: {
     "UserAccount": {   
        (payload.UserResponse.UserDetailsResp.UserAccounts map {
            Address:{
              <XMLFieldName>: $.<respectiveJSONFieldToMap>
              ....
            } 
        })
     }
   }
 } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM