WSO2 ESB 5中的动态json-eval表达式 [英] Dynamic json-eval expressions in WSO2 ESB 5
问题描述
我正在尝试使用动态生成的json-eval处理JSONPayload来选择一个垂直的酒店对象. 下面提到的直接json-eval可以正常工作.
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.
直接json-eval表达式:
json-eval($.content[?(@.hotelcode=='ALE1_LON')])
我尝试过类似beow的选项,但是还没有运气.
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"/>
这使用"{$ctx:xpathExpr}
"作为JSON路径,而不是"$.content[?(@.hotelcode=='ALE1_LON')]
".
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"/>
这会将"json-eval($.content[?(@.hotelcode=='ALE1_LON')])
"存储到hotelContet属性中,而没有对其进行评估.
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"/>
这使用"$.content[?(@.hotelcode=={get-property('htlCode')})]
"作为JSON路径,而不是"$.content[?(@.hotelcode=='ALE1_LON')]
".
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"/>
这使用"$.content[?(@.hotelcode=={$ctx.htlCode})]
"作为JSON路径,而不是"$.content[?(@.hotelcode=='ALE1_LON')]
".
This uses the "$.content[?(@.hotelcode=={$ctx.htlCode})]
" as the JSON Path instead of "$.content[?(@.hotelcode=='ALE1_LON')]
".
Json有效载荷:
{
"_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"
}
]
}
注意:我知道可以使用脚本/类调解器完成此操作.但是我正在寻找json-eval中的解决方案.而且,如果我可以限制为JSONPath而不是XPath,那就更好了.
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.
目前,我正在通过以下类似方法来管理JSON.
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版本:5.0.0
推荐答案
请测试此解决方案以解决您的问题
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>
这篇关于WSO2 ESB 5中的动态json-eval表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!