Array to flat mapping in Mule based on condition

Viewed 1353

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.

3 Answers

You should use the Group By operation to transform your XML file into required JSON format. you can do something like this:

Example of DataWeave Transformation:

%dw 1.0
%output application/dw
---
classrooms: payload.school.teachers groupBy $.subject mapObject ((teacherGroup, subject) -> {
    class: {
      name: subject,
      teachers: { (teacherGroup map {
        teacher:{
            name: $.name,
            lastName: $.lastName
        }
      }) },
      attendees: { (payload.school.students filter ($.*hobby contains subject) map {
        attendee: {
          name: $.name,
          lastName: $.lastName
        }
      }) }
    }
})
In Case of Mule in case of single XML element convert to Array 

xml files : 

<usrId>10120</usrId>

IN Rule file use double dot($..usrId) instead ofsinfle ($.usrId) it will revert array element for single element.

RULE FILE:
<parameter parameter_name_client="$..usrId"
parameter_name_server="facture_ids" format_required="false"
data_type="Array" value_change_required="false"
is_TLV="false" is_mandatory="false" to_be_logged="false"/>
Related