I am working on a WSO2 ESB project and I am finding the following difficulties:
I have a JSON document like this:
{
"forecast_1": {
"country": "Rwanda",
"forecast_date": "2018-03-20",
"province": "Kigali City",
"district": "Kigali",
"morning": {
"min_temp": 14,
"status": "Sun with right rain",
"max_temp": 16,
"humidity": "",
"wind_direction": "",
"wind_force": "",
"description": "Lorem ipsum"
},
"afternoon": {
"min_temp": 24,
"status": "Dark cloud with rain",
"max_temp": 28,
"humidity": "",
"wind_direction": "",
"wind_force": "",
"description": "Lorem ipsum"
}
},
"forecast_2": {
"country": "Rwanda",
"forecast_date": "2018-03-25",
"province": "Kigali",
"district": "Kigali",
"morning": {
"min_temp": 21,
"status": "Rain showers",
"max_temp": 21,
"humidity": "",
"wind_direction": "",
"wind_force": "",
"description": "There will be heavy rain"
},
"afternoon": {
"min_temp": 32,
"status": "Rain showers",
"max_temp": 32,
"humidity": "",
"wind_direction": "",
"wind_force": "",
"description": "There will be heavy rain with thunder"
}
}
}
Unfortunately this JSON document doesn't contains an array of objects but contains some forecast_XXX objects, where XXX will change.
I have to iterate on each forecast_XXX objects (the first level objects) defined into this JSON document and extracts the values of the fields.
To iterate on these "first level" objects I am doing in this way (and it seems to work):
<foreach id="foreach_1" expression="//*[starts-with(name(), 'forecast_')]" xmlns:m0="http://services.samples">
<sequence>
<log description="Log" level="custom">
<property name="iteration" value="ITERATION !!!"/>
</log>
<enrich>
<source clone="true" xpath="/country/text()"/>
<target property="test" type="property"/>
</enrich>
<log description="Log" level="custom">
<property expression="$ctx:test" name="test"/>
</log>
<log level="full"/>
</sequence>
</foreach>
The iteration seems to works because in my WSO2 logs I obtain:
TID: [-1234] [] [2018-03-29 13:51:50,271] INFO {org.apache.synapse.mediators.builtin.LogMediator} - iteration = ITERATION !!! {org.apache.synapse.mediators.builtin.LogMediator}
TID: [-1234] [] [2018-03-29 13:51:50,271] ERROR {org.apache.synapse.mediators.elementary.EnrichMediator} - Specified node by xpath cannot be found. {org.apache.synapse.mediators.elementary.EnrichMediator}
TID: [-1234] [] [2018-03-29 13:51:50,272] INFO {org.apache.synapse.mediators.builtin.LogMediator} - test = {org.apache.synapse.mediators.builtin.LogMediator}
TID: [-1234] [] [2018-03-29 13:51:50,272] INFO {org.apache.synapse.mediators.builtin.LogMediator} - To: /meteo/forecast, MessageID: urn:uuid:21779fb7-6ce6-4e90-a9cb-4187dab8988b, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><forecast_date>2018-03-20</forecast_date></soapenv:Body></soapenv:Envelope> {org.apache.synapse.mediators.builtin.LogMediator}
TID: [-1234] [] [2018-03-29 13:51:50,272] INFO {org.apache.synapse.mediators.builtin.LogMediator} - iteration = ITERATION !!! {org.apache.synapse.mediators.builtin.LogMediator}
TID: [-1234] [] [2018-03-29 13:51:50,272] ERROR {org.apache.synapse.mediators.elementary.EnrichMediator} - Specified node by xpath cannot be found. {org.apache.synapse.mediators.elementary.EnrichMediator}
TID: [-1234] [] [2018-03-29 13:51:50,273] INFO {org.apache.synapse.mediators.builtin.LogMediator} - test = {org.apache.synapse.mediators.builtin.LogMediator}
So, as you can see, I have 2 logs related to the current iteration, something lie this:
TID: [-1234] [] [2018-03-29 13:51:50,271] INFO {org.apache.synapse.mediators.builtin.LogMediator} - iteration = ITERATION !!! {org.apache.synapse.mediators.builtin.LogMediator}
But now I am finding some difficulties to extract the field values related to the current object iteration.
I tried to do in this way:
<enrich>
<source clone="true" xpath="/country/text()"/>
<target property="test" type="property"/>
</enrich>
<log description="Log" level="custom">
<property expression="$ctx:test" name="test"/>
</log>
but, as you can see in the log, the enrich mediato go into error and the "test" property will contain no value:
TID: [-1234] [] [2018-03-29 13:51:50,271] ERROR {org.apache.synapse.mediators.elementary.EnrichMediator} - Specified node by xpath cannot be found. {org.apache.synapse.mediators.elementary.EnrichMediator}
TID: [-1234] [] [2018-03-29 13:51:50,272] INFO {org.apache.synapse.mediators.builtin.LogMediator} - test = {org.apache.synapse.mediators.builtin.LogMediator}
So what is wrong? Why the XPATH expression is wrong? How can I correctly extracts the field values of the current iteration object? (I am not so sure that using the enrich mediator is the best choice).