使用表达式中对象内的数组

时间:2013-12-03 16:50:27

标签: mule mule-el

早上好!我正在尝试使用Mule创建一个Web API,它将一个基于JSON的请求从系统A转换为另一个基于JSON的格式,使用转换的JSON在系统B上调用Web API,从系统B获取JSON响应并转换它是系统A可以理解的一些JSON,并将结果返回给原始HTTP请求的响应。我主要使用它,但我无法弄清楚如何在属性中引用数组。这是进入Expression组件的JSON:

{
    "FooObjects":
    [
        {
            "FooPropertyOne": "value for property one"
          , "FooPropertyTwo": "value for property two"
          , "FooPropertyThree": "value for property three"
          , "FooId": 22031
          , "FooBarRelationships":
            [
                {
                    "FooBarRelationshipId": 18014
                  , "RelationshipProperty": "value for rel property"
                  , "Bar": { "BarPropertyOne": "value for property one", "BarId": 11379 }
                }
              , {
                    "FooBarRelationshipId": 18015
                  , "RelationshipProperty": "value for rel property"
                  , "Bar": { "BarPropertyOne": "value for property one", "BarId": 10001 }
                }
            ]
        }
    ]
}

这是我想要的结果:

{
    "FooObjects":
    [
        {
            "FooPropertyOne": "value for property one"
          , "FooPropertyTwo": "value for property two"
          , "FooPropertyThree": "value for property three"
          , "FooId": 22031
          , "FooUrl": "https://server/path/to/foo?FooId=22013"
          , "Bars":
            [
                { "BarPropertyOne": "value for property one", "BarId": 11379 }
              , { "BarPropertyOne": "value for property one", "BarId": 10001 }
            ]
        }
    ]
}

这是表达式组件:

    <expression-component doc:name="Expression"><![CDATA[payload =
[
    "FooObjects": ([
        "FooPropertyOne": $.FooPropertyOne
      , "FooPropertyTwo": $.FooPropertyTwo
      , "FooPropertyThree": $.FooPropertyThree
      , "FooId": $.FooId
      , "FooURl": "https://server/path/to/foo?FooId=" + $.FooId
      , "Bars": $.FooBarRelationships
    ] in payload.FooObjects)
];]]></expression-component>

我一直在猜测代替$.FooBarRelationships的语法。我能想到的最好的是(['BarPropertyOne':$.Bar.BarPropertyOne]in $.FooBarRelationships)。这让我:

[Error: could not access: Bar; in class: java.util.LinkedHashMap]
[Near : {... $.Bar.BarPropertyOne....}]
             ^

我该如何进行这种转变?如果我遗漏关于Bars的部分,除了没有得到我需要的所有数据之外,一切正常。谢谢!

编辑:我更改了示例以更清晰地显示多重性。

1 个答案:

答案 0 :(得分:1)

我能够完成这项工作的唯一方法是通过一个功能:

<configuration>
    <expression-language>
        <global-functions>
            def makeBars(fooBarRelationships) {
              (['BarPropertyOne':$.Bar.BarPropertyOne] in fooBarRelationships)
            }
        </global-functions>
    </expression-language>
</configuration>

然后在流程中我可以使用:

<expression-component doc:name="Expression"><![CDATA[payload =
    [
        "FooObjects": ([
            "FooPropertyOne": $.FooPropertyOne
          , "FooPropertyTwo": $.FooPropertyTwo
          , "FooPropertyThree": $.FooPropertyThree
          , "FooId": $.FooId
          , "FooURl": "https://server/path/to/foo?FooId=" + $.FooId
          , "Bars": makeBars($.FooBarRelationships)
        ] in payload.FooObjects)
    ];]]></expression-component>