Tuesday, April 5, 2016

Array to flat mapping in Mule based on condition

Leave a Comment

I am attempting to map a response from a 3rd party call into a different structure using the DataMapper transform within Mule.

From the 3rd party we receive an array of items (amongst other things) and I want to map a single item within the array to an object (JSON). I receive an identifier for the item in the request which is available as an input argument.

My question is, how can I map the fields of the item based on the identifier?

Example XML response

<account>     <accountNumber>1234567</accountNumber>     <books>         <book>             <id>1</id>             <title>Sample title1</title>             <availableFrom>21-03-16</availableFrom>         </book>         <book>             <id>2</id>             <title>Sample title2</title>             <availableFrom>21-03-16</availableFrom>         </book>         <book>             <id>3</id>             <title>Sample title3</title>             <availableFrom>21-03-16</availableFrom>         </book>     </books> </account> 

Needs to become:

{     "person": {         "accountNumber": 1234567,         "selectedBook": {             "id": 1,              "title": "Sample title1"         },         "otherBooks": [             {                 "id": 2,                  "title": "Sample title2"             },             {                 "id": 3,                  "title": "Sample title3"             }         ]     } } 

The id of the book selected is held in an inputArgument.bookId.

I can complete the mapping using an xpath rule for each of the fields, however, I have to hardcode the bookId in the xpath. Instead, I need to be able to substitute the actual id for the one provided (and available in the inputArgument).

xpath for title

/account/books/book[child::id=1]/title 

I have tried adding MEL to replace the id and various other fixes but nothing seems to work. Is there a way to substitute the bookId field.

Note: Due to client restrictions, I cannot use DataWeave.

0 Answers

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment