2017-03-08 73 views
0

我试图使用动态生成的json-eval来选择一个特定的酒店对象来处理JSONPayload。 下面提到的直接json-eval工作正常。WSO2 ESB中的动态json-eval表达式5

直接JSON-EVAL表达:

json-eval($.content[?(@.hotelcode=='ALE1_LON')]) 

我已经试过beow类似的选项,但还没有任何运气。

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')]”。

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属性,而不eveluating它。

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')]”。

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')]”。

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,那更好。

目前我正在管理自己使用下面类似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

回答

0

请测试这个解决方案为您解决问题

<?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=','&#34;',get-property('htlCode'),'&#34;',']')" 
        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> 
+0

谢谢你的答案。是的,我可以通过JSON来处理XPath,正如我在后面提到的那样。但我期待在json-eval函数中使用JSONPath做到这一点。 – namalfernandolk