0
votes

I tried to extract json array data using message enricher. But in output I got some object data.How can I fetch properly?

Following is my flow

<flow name="readfileFlow1" doc:name="readfileFlow1">
        <file:inbound-endpoint path="Test" moveToDirectory="Backup" responseTimeout="10000" doc:name="File"/>
        <enricher source="#[(name in payload.data)]" target="#[flowVars.myMap]" doc:name="Message Enricher">
            <json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>
        </enricher>
        <logger message="#[payload.toString()]" level="INFO" doc:name="Logger"/>
</flow>

When I print in logger then I got following output

INFO  2015-04-29 13:01:20,409 [[readfile].readfileFlow1.stage1.02] org.mule.api.processor.LoggerMessageProcessor: org.mule.transport.file.ReceiverFileInputStream@15bc5cc

How can I extract properly all name I tried using for each . Using for each I can extract succesfully. But want to use message enricher. How can I do this using message enricher MVEL??

My json data is as follow..

{
   "data":[
       {
            "id" : "1",
    "name": "AAA"

        },
        {
            "id" : "5",
                "name": "DDD"
        },
        {
            "id" : "6",
                "name": "CCC"
        },


    ]
}
2

2 Answers

1
votes

I have done this.. silly mistake I made.. Following is my answer

<logger message="#[flowVars.myMap]" level="INFO" doc:name="Logger"/>
0
votes

This is how you can achieve to extract name and id from your JSON payload .. you will get it in logger

   <flow name="readfileFlow1" doc:name="readfileFlow1">
     <file:inbound-endpoint path="Test" moveToDirectory="Backup" responseTimeout="10000" doc:name="File"/> 
     <enricher source="#[message.payload]" target="#[flowVars.myMap]" doc:name="Message Enricher">
       <processor-chain doc:name="Processor Chain">
       <json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>
       <foreach collection="#[payload.data]">
       <logger level="INFO" message="Name :- #[payload.name] and Id:- #[payload.id]"/>
       </foreach>
       </processor-chain>
      </enricher>
     <logger level="INFO" message="#[flowVars.myMap]"/>     
    </flow>

One more thing you JSON payload is not valid.. try to remove an extra , at the end .. so your valid JSON file will be :-

{
   "data":[
       {
            "id" : "1",
    "name": "AAA"

        },
        {
            "id" : "5",
                "name": "DDD"
        },
        {
            "id" : "6",
                "name": "CCC"
        }
    ]
}