2017-02-17 10 views
1

results配列を反復処理し、レスポンス配列を構築するには、carValueとbikeValueをid( "C_05" - "B_08"/"C_07"/"B_06")に追加します。レスポンス配列と記述と同じdescを保持します。JSONペイロードを反復処理してwso2でレスポンスを構成する方法

JSONペイロード要求:

{"results": [ 
     { 
     "desc": "Blind", 
     "carValue": "05", 
     "bikeValue": "08" 
    }, 
     { 
     "desc": "Deaf", 
     "carValue": "09", 
     "bikeValue": "10" 
    }, 
{ 
     "desc": "Oxygen", 
     "carValue": "07" 
    }, 
{ 
     "desc": "carbon", 
     "bikeValue": "06" 
    }] 
} 

最終的な応答は次のようになります。

{ 
    "Response" : [ 
    { 
    "id" : "C_05"-"B_08", 
    "description" : "Blind" 
    }, 
    { 
    "id" : "C_09"-"B_10", 
    "description" : "Deaf" 
    }, 
    { 
    "id": "C_07", 
    "description": "Oxygen" 
    }, 
    { 
    "id": "B_06", 
    "description": "carbon" 
    }] 
} 
+0

使用しているWSO2ESBのバージョンを生成するために、カスタムまたはpayloadFactoryメディエーターを使用することができますか? –

+0

そのwso2esb - 5.0.0 – sam

+0

あなたはDatamapperメディエーターを使用することができます:https://docs.wso2.com/display/ESB500/WSO2+ESB+Data+Mapper+JSON+Schema+Specification –

答えて

4

私はこのプロキシでお見せしてあなたは、反復、集計およびスクリプトメディエーターを使用することができます。

<?xml version="1.0" encoding="UTF-8"?> 
<proxy xmlns="http://ws.apache.org/ns/synapse" 
     name="proxyIterateJson" 
     startOnLoad="true" 
     statistics="disable" 
     trace="disable" 
     transports="http,https"> 
    <target> 
     <inSequence> 
     <payloadFactory media-type="json"> 
      <format> 
             {"results": [&#xD; 
                {&#xD; 
                "desc": "Blind",&#xD; 
                "carValue": "05",&#xD; 
                "bikeValue": "08"&#xD; 
              },&#xD; 
                {&#xD; 
                "desc": "Deaf",&#xD; 
                "carValue": "09",&#xD; 
                "bikeValue": "10"&#xD; 
              },&#xD; 
             {&#xD; 
                "desc": "Oxygen",&#xD; 
                "carValue": "07"&#xD; 
              },&#xD; 
             {&#xD; 
                "desc": "carbon",&#xD; 
                "bikeValue": "06"&#xD; 
              }]&#xD; 
             } 
         </format> 
      <args/> 
     </payloadFactory> 
     <iterate xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
        attachPath="//jsonObject" 
        expression="//jsonObject/results" 
        id="it1" 
        preservePayload="true"> 
      <target> 
       <sequence> 
        <property expression="$body//jsonObject/results/carValue" 
          name="carValue" 
          scope="default" 
          type="STRING"/> 
        <property expression="$body//jsonObject/results/bikeValue" 
          name="bikeValue" 
          scope="default" 
          type="STRING"/> 
        <property expression="$body//jsonObject/results/desc" 
          name="description" 
          scope="default" 
          type="STRING"/> 
        <script language="js">var carValue = mc.getProperty("carValue"); 
             var bikeValue = mc.getProperty("bikeValue"); 
             var desc = mc.getProperty("description"); 
             var concatValue; 
             if(carValue == null || carValue == "") { 
              concatValue = "B_" + bikeValue; 
             } else if(bikeValue == null || bikeValue == ""){ 
              concatValue = "C_" + carValue; 
             }else { 
              concatValue = "C_"+ carValue + "-" + "B_" + bikeValue; 
             } 
             print("Value concatenado: "+ concatValue); 
             mc.setProperty("concatValue", concatValue); 
             mc.setPayloadJSON({"result":{"id" : concatValue,"description" : desc}});</script> 
        <log> 
        <property expression="json-eval($.result.id)" name="JSON-Payload"/> 
        </log> 
        <loopback/> 
       </sequence> 
      </target> 
     </iterate> 
     </inSequence> 
     <outSequence> 
     <property name="res" scope="default"> 
      <ns:Response xmlns:ns="www.response.org"/> 
     </property> 
     <aggregate id="it1"> 
      <completeCondition> 
       <messageCount max="-1" min="-1"/> 
      </completeCondition> 
      <onComplete enclosingElementProperty="res" expression="$body//result"> 
       <log level="custom" separator=","> 
        <property expression="json-eval($.)" name="OUPUTSECUENSE"/> 
       </log> 
       <send/> 
      </onComplete> 
     </aggregate> 
     </outSequence> 
     <faultSequence/> 
    </target> 
    <description/> 
</proxy> 

これは、応答

{"Response": 
    {"result":[ 
     {"id":"C_09-B_10","description":"Deaf"}, 
     {"id":"C_05-B_08","description":"Blind"}, 
     {"id":"C_07","description":"Oxygen"}, 
     {"id":"B_06","description":"carbon"} 
]}} 

ます。また、JSONレスポンス

https://docs.wso2.com/display/ESB500/Writing+a+WSO2+ESB+Mediator

+0

あなたの答えをありがとう、私の自由な時間にあなたのソリューションを試してみます。私は単純なスクリプトメディエーターを使用して、APIとその作業のためにそれを呼び出すことを試みた。 – sam

関連する問題