Unflattening view data to JSON with Mule DataMapper

冷暖自知 提交于 2019-12-25 03:11:40

问题


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:

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.


回答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 />


来源:https://stackoverflow.com/questions/17430254/unflattening-view-data-to-json-with-mule-datamapper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!