问题描述
我有一个数据库视图,需要将其发送到JSON格式的Web API调用,但是我很难确定如何获取数据映射器以将数据展平。我想获取的格式如下:
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.
谢谢!
edit 这是流程的图片:
edit Here's a picture of the flow:
民意调查和JDBC是从MY_VIEW中选择不同的人名
。在for每个应用程序中,我希望每个人进行一次JSON调用。我现在正在输出到文件,而不是调用Web API,尽管我确实尝试了Web API调用,但只要获得有效的JSON,它就可以正常工作。
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.
推荐答案
我确实意识到您已要求DataMapper解决方案。如果需要替代解决方案,则以下仅使用MEL:
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 />
这篇关于使用Mule DataMapper将视图数据平展为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!