Dynamic json-eval expressions in WSO2 ESB 5

僤鯓⒐⒋嵵緔 提交于 2019-12-13 00:38:00

问题


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=','&#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>


来源:https://stackoverflow.com/questions/42669436/dynamic-json-eval-expressions-in-wso2-esb-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!