0

I've got a database view that I need to send to a Web API call in JSON format, but I'm having a hard time figuring out how to get the datamapper to un-flatten the data. The format I want to get to is something like:

{
    "PersonId": "12345"
  , "CommonProp": "asdf"
  , "DataForPerson": [
        { "Prop1": "prop 1 value A", "Prop2": "prop 2 value A" }
      , { "Prop1": "prop 1 value B", "Prop2": "prop 2 value B" }
    ]
}

The format coming in from the view is something like:

PersonId    CommonProp    Prop1             Prop2
12345       asdf          prop 1 value A    prop 2 value A
12345       asdf          prop 2 value B    prop 2 value B

How can I go about doing this? The closest I've gotten is

{
    "PersonId": "12345"
  , "CommonProp": "asdf"
  , "DataForPerson": [
        { "Prop1": "prop 1 value A", "Prop2": "prop 2 value A" }
    ]
} {
    "PersonId": "12345"
  , "CommonProp": "asdf"
  , "DataForPerson": [
        { "Prop1": "prop 1 value B", "Prop2": "prop 2 value B" }
    ]
}

Obviously, this is not correct. I'd tried to use the datamapper to do this, but didn't have any luck.

Thanks!

edit Here's a picture of the flow:

Mule Flow

The poll and the JDBC are a SELECT DISTINCT PERSON_ID FROM MY_VIEW. In the for each, I was hoping to make one JSON call per person. I'm outputting to a file right now instead of invoking the Web API, though I did try the Web API call, and it works fine so long as it gets valid JSON.

Becca Dee
  • 1,530
  • 1
  • 24
  • 51

1 Answers1

1

I do realize that you've asked for a DataMapper solution. In case you want an alternate solution, the following uses MEL only:

<expression-component><![CDATA[
    payload =
      [
        'PersonId': payload[0].PersonId,
        'CommonProp': payload[0].CommonProp,
        'DataForPerson': (['Prop1': $.Prop1, 'Prop2': $.Prop2] in payload)
      ];
]]></expression-component>

<json:object-to-json-transformer />
David Dossot
  • 33,403
  • 4
  • 38
  • 72
  • This works, @David Dossot. Do you think that there is no way to do this using a DataMapper? – Becca Dee Jul 03 '13 at 17:49
  • It certainly possible to use DataMapper for this but I'm no expert with it: hopefully someone else will post a solution. Since you're an Enterprise Edition user you can also contact MuleSoft's professional support. – David Dossot Jul 03 '13 at 18:15