Extracting array from JSON in mule esb

后端 未结 2 1177
梦如初夏
梦如初夏 2021-01-15 05:12

I\'m using Mule 3.4 CE and I have a JSON data coming through HTTP in the following format:

{
   \"People\" : [
    {
       \"Details\" : 
       {
                  


        
相关标签:
2条回答
  • 2021-01-15 05:48

    There best way to query json is to transform it to a Map.

    <json:json-to-object-transformer returnClass="java.util.HashMap" />
    

    And then query it using MEL like standard MVEL or Java syntax

    <logger message="#[payload.People[0].Details.email]" level="INFO" />
    

    If you want to keep the original json payload intact, you can store the map in a variable using an enricher:

    <enricher target="#[flowVars.myJsonMap]">
       <json:json-to-object-transformer returnClass="java.util.HashMap" />
    </enricher>
    

    And query the variable instead of the payload:

    <logger message="#[flowVars.myJsonMap.People[0].Details.email]" level="INFO" />
    

    You could also map the json to a custom class using Jackson and change the returnClass attribute to your class.

    This MEL cheat sheet detail JSON processing with MEL and also how to handle Maps, arrays etc: http://www.mulesoft.org/documentation/display/current/MEL+Cheat+Sheet

    Note: You may come across a #[json:] evaluator, but this is deprecated in favour of the approach above.

    UPDATE:

    If you want to grab all the emails at once you can use MVEL projections:

    <enricher target="#[flowVars.myJsonMap]" source="#[(Details.email in payload.People)]">
           <json:json-to-object-transformer returnClass="java.util.HashMap" />
    
        </enricher>
    

    Mvel projections: http://mvel.codehaus.org/MVEL+2.0+Projections+and+Folds

    0 讨论(0)
  • 2021-01-15 05:58

    If you want to get the particular email in the details use the below MEL expression.

    #[json:People[0]/Details/Email]
    

    If you want to get all the emails, pass the json path of repeatable in ForEach Collections then pass the path to get Email as shown below.

     <flow name="parsingjsonFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <foreach collection="#[json:People]" doc:name="For Each">
            <logger message="#[json:/Details/Email]" level="INFO" doc:name="Logger"/>
        </foreach>
        <catch-exception-strategy doc:name="Catch Exception Strategy">
            <logger message="#[exception.getClass()]" level="INFO" doc:name="Logger"/>
        </catch-exception-strategy>
    </flow>
    
    0 讨论(0)
提交回复
热议问题