问题
I'm trying to process a JSONPayload using a dynamically generated json-eval to select a perticular hotel object. Below mentioned direct json-eval works fine.
Direct json-eval expression :
json-eval($.content[?(@.hotelcode=='ALE1_LON')])
I have tried beow like options but no any luck yet.
TRY 1 :
<property name="htlCode" scope="default" type="STRING" value="'ALE1_LON'"/>
<property expression="fn:concat('$.content[?(@.hotelcode==',get-property('htlCode'),')]')" name="xpathExpr" scope="default" type="STRING"/>
<property expression="json-eval({$ctx:xpathExpr})" name="hotelContet" scope="default" type="STRING"/>
This uses the "{$ctx:xpathExpr}
" as the JSON Path instead of "$.content[?(@.hotelcode=='ALE1_LON')]
".
TRY 2 :
<property name="htlCode" scope="default" type="STRING" value="'ALE1_LON'"/>
<property expression="fn:concat('json-eval($.content[?(@.hotelcode==',get-property('htlCode'),')])')" name="hotelContet" scope="default" type="STRING"/>
This stores "json-eval($.content[?(@.hotelcode=='ALE1_LON')])
" to the hotelContet property without eveluating it.
TRY 3 :
<property name="htlCode" scope="default" type="STRING" value="'ALE1_LON'"/>
<property expression="json-eval($.content[?(@.hotelcode=={get-property('htlCode')})])" name="hotelContet" scope="default" type="STRING"/>
This uses the "$.content[?(@.hotelcode=={get-property('htlCode')})]
" as the JSON Path instead of "$.content[?(@.hotelcode=='ALE1_LON')]
".
TRY 4 :
<property name="htlCode" scope="default" type="STRING" value="'ALE1_LON'"/>
<property expression="json-eval($.content[?(@.hotelcode=={$ctx.htlCode})])" name="hotelContet" scope="default" type="STRING"/>
This uses the "$.content[?(@.hotelcode=={$ctx.htlCode})]
" as the JSON Path instead of "$.content[?(@.hotelcode=='ALE1_LON')]
".
Json Payload :
{
"_id":"INV27_1112",
"_rev":"5-876038bf65752ce4505e50baea6d5581",
"content":[
{
"hotelcode":"AMB3_LON",
"hotelname":"Ambassadors Bloomsbury"
},
{
"hotelcode":"ALE1_LON",
"hotelname":"Alexandra"
},
{
"hotelcode":"ALO_LON",
"hotelname":"Aloft London Excel"
}
]
}
Note : I know that this can be done with Script / Class mediator. But I'm looking for a solution within the json-eval. And better if I can limit to JSONPath rather than XPath.
Currently I'm managing myself using below like approach over JSON.
<property name="htlCode" scope="default" type="STRING" value="'ALE1_LON'"/>
<property expression="fn:concat('//content[hotelcode=',$ctx:htlCode,']')" name="hotelContentExpr" scope="default" type="STRING"/>
<property expression="evaluate($ctx:hotelContentExpr)" name="hotelContent" scope="default" type="STRING"/>
- WSO2 ESB version : 5.0.0
回答1:
Please test this solution to solve your problem
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="JsonDynamicExpression"
startOnLoad="true"
statistics="disable"
trace="disable"
transports="http,https">
<target>
<inSequence>
<payloadFactory media-type="json">
<format>{
"_id":"INV27_1112",
"_rev":"5-876038bf65752ce4505e50baea6d5581",
"content":[
{
"hotelcode":"AMB3_LON",
"hotelname":"Ambassadors Bloomsbury"
},
{
"hotelcode":"ALE1_LON",
"hotelname":"Alexandra"
},
{
"hotelcode":"ALO_LON",
"hotelname":"Aloft London Excel"
}
]
}</format>
<args/>
</payloadFactory>
<property name="htlCode" scope="default" type="STRING" value="ALO_LON"/>
<property expression="fn:concat('//content[hotelcode=','"',get-property('htlCode'),'"',']')"
name="hotelContentExpr"
scope="default"
type="STRING"/>
<enrich>
<source clone="true" xpath="evaluate(get-property('hotelContentExpr'))"/>
<target type="body"/>
</enrich>
<log>
<property expression="$body" name="Cuerpo////////////////////////////"/>
</log>
<loopback/>
</inSequence>
<outSequence>
<send/>
</outSequence>
</target>
<description/>
</proxy>
来源:https://stackoverflow.com/questions/42669436/dynamic-json-eval-expressions-in-wso2-esb-5