【问题标题】:Mule Server 3.6 > Anypoint Studio > Data ExtractionMule Server 3.6 > Anypoint Studio > 数据提取
【发布时间】:2015-08-29 05:39:58
【问题描述】:

当我有一个包含以下内容的 sessionVars.filters 时,如何针对 JSON 数据提取信息:

["account", "billing"]

...我的 JSON 数据包含在哪里:

{
    "billing": {
        "BillNumber": 25,
        "BillPeriod": "06 Dec 14 - 05 Jan 15",
        "AccountNumber": 78781843,
        "PreviousBalance": 0.00,
        "CurrentBalance": 1237.49,
        "DueDate": "Jan 26, 2015",
        "TotalAmountDue": 1237.49,
        "PreviousBalance": 0.00,
        "CurrentBalance": 1237.49,
        "DueDate": "Jan 26, 2015",
        "TotalAmountDue": "1237.49"
    },
    "product": ["hilly"],
    "account": {
        "Name": "Lee G. Ive",
        "Address": "214 Maya St., G2 Village Highlands City, Somewhere 1630"
    },
    "content": {
        "package": {
            "userId": "1234"
        },
        "service": {
            "username": "hershey123"
        }
    }
}

...由此,我希望能够返回以下有效负载(使用我的过滤器数组):

{
    "billing": {
        "BillNumber": 25,
        "BillPeriod": "06 Dec 14 - 05 Jan 15",
        "AccountNumber": 78781843,
        "PreviousBalance": 0.00,
        "CurrentBalance": 1237.49,
        "DueDate": "Jan 26, 2015",
        "TotalAmountDue": 1237.49,
        "PreviousBalance": 0.00,
        "CurrentBalance": 1237.49,
        "DueDate": "Jan 26, 2015",
        "TotalAmountDue": "1237.49"
    },
    "account": {
        "Name": "Lee G. Ive",
        "Address": "214 Maya St., G2 Village Highlands City, Somewhere 1630"
    }
}

编辑:附录

这就是我在 Anypoint Studio 中尝试做的事情:

response = {
    "result": {
        "code": 200,
        "status": "success"
    }
};
foreach (sessionVars.filters as filter) {
    response[filter] = data[filter];
}

从那个“伪代码”中,我可以创建我的最终响应负载:

{
    "result": {
        "code": 200,
        "status": "success"
    },
    "billing": {
        "BillNumber": 25,
        "BillPeriod": "06 Dec 14 - 05 Jan 15",
        "AccountNumber": 78781843,
        "PreviousBalance": 0.00,
        "CurrentBalance": 1237.49,
        "DueDate": "Jan 26, 2015",
        "TotalAmountDue": 1237.49,
        "PreviousBalance": 0.00,
        "CurrentBalance": 1237.49,
        "DueDate": "Jan 26, 2015",
        "TotalAmountDue": "1237.49"
    },
    "account": {
        "Name": "Lee G. Ive",
        "Address": "214 Maya St., G2 Village Highlands City, Somewhere 1630"
    }
}

我忘了提到 sessionVars.filters 数组是动态的,具体取决于请求,它只能包含 ["billing"] 或 ["product"、"account"] 等。

【问题讨论】:

    标签: json mule esb mule-studio


    【解决方案1】:

    你可以用一个

    <transformer ref="StringToObject" returnClass="java.util.HashMap"/>
    

    有了地图后,只需访问密钥即可。也许像

    #[payload.billing]
    

    #[payload.account]
    

    基本上 JSON 路径在 Mule 中有点受限,因此您宁愿将 JSON 转换为 HashMap 并使用 MEL 或以编程方式查询它们。

    附录

    下面是一个虚拟流程,它使用脚本转换器从有效负载中过滤掉不需要的字段,它本质上是您的逻辑的实现。

    <flow name="soapwsFlow2">
            <http:listener config-ref="HTTP_Listener_Configuration" path="/transformer" doc:name="8081/transformer"/>
            <set-payload value="#[['accounts':'lots of accountingJson','billing':'even more billing json','somethingelse':'lots of other stuff']]" doc:name="Set Payload"/>
            <set-session-variable variableName="filters" value="#[['accounts','billing']]" doc:name="Session Variable"/>
            <scripting:transformer returnClass="java.util.HashMap" doc:name="Groovy Transformer">
                <scripting:script engine="Groovy"><![CDATA[Map payloadData = payload
    Map filteredData = new HashMap();
    for(String filter : sessionVars.filters){
        filteredData.put(filter,payloadData[filter]);
    }
    return filteredData]]></scripting:script>
            </scripting:transformer>
            <json:object-to-json-transformer doc:name="Object to JSON"/>
            <logger message="FInal Payload #[payload]" level="INFO" doc:name="Logger"/>
        </flow>
    

    【讨论】:

    • Cool @Sudarshan!... 然后我可以使用 For Each 范围迭代 HashMap 并检查我的 sessionVars.filters 数组中是否存在下一个索引,对吗?我如何能够将提取的信息推送并附加到我的返回负载中?还是有比我想象的更好的方法?
    • 与大多数情况一样,还有其他方法,但您的方法似乎也不错。将数据附加到 mule 有效负载 here 或考虑使用 message enricher
    • 好的...如果我要使用 For Each 范围,我将如何创建最终的有效负载?我想我现在迷路了......一直在尝试使用它,但我无法实现我想要做的事情。另一个例子真的很有帮助。
    • 在答案中添加了详细信息,如果这有帮助,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 2015-08-29
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    • 2016-04-05
    • 2018-08-20
    • 1970-01-01
    • 2016-07-29
    相关资源
    最近更新 更多