title: WSO2 WebSocket cannot parse JSON string
body: <p>Since WSO2 5.0 supports WebSockets, I have written a simple app following by tutorials:</p>
  
  <ul>
  <li><a href="https://docs.wso2.com/display/ESB500/WebSocket+Transport" rel="nofollow">WebSocket Transport</a> </li>
  <li><a href="https://docs.wso2.com/display/ESB500/Sending+a+Message+from+a+WebSocket+Client+to+an+HTTP+Endpoint" rel="nofollow">Sending a Message from a WebSocket Client to an HTTP Endpoint</a></li>
  </ul>
  
  <p>Here's my source view output from WSO2:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;definitions xmlns="http://ws.apache.org/ns/synapse"&gt;
      &lt;registry provider="org.wso2.carbon.mediation.registry.WSO2Registry"&gt;
          &lt;parameter name="cachableDuration"&gt;15000&lt;/parameter&gt;
      &lt;/registry&gt;
      &lt;taskManager provider="org.wso2.carbon.mediation.ntask.NTaskTaskManager"/&gt;
      &lt;sequence name="fault"&gt;
          &lt;!-- Log the message at the full log level with the ERROR_MESSAGE and the ERROR_CODE--&gt;
          &lt;log level="full"&gt;
              &lt;property name="MESSAGE" value="Executing default 'fault' sequence"/&gt;
              &lt;property expression="get-property('ERROR_CODE')" name="ERROR_CODE"/&gt;
              &lt;property expression="get-property('ERROR_MESSAGE')" name="ERROR_MESSAGE"/&gt;
          &lt;/log&gt;
          &lt;!-- Drops the messages by default if there is a fault --&gt;
          &lt;drop/&gt;
      &lt;/sequence&gt;
      &lt;sequence name="main"&gt;
          &lt;in&gt;
              &lt;!-- Log all messages passing through --&gt;
              &lt;log level="full"/&gt;
              &lt;!-- ensure that the default configuration only sends if it is one of samples --&gt;
              &lt;!-- Otherwise Synapse would be an open proxy by default (BAD!)               --&gt;
              &lt;filter regex="http://localhost:9000.*" source="get-property('To')"&gt;
                  &lt;!-- Send the messages where they have been sent (i.e. implicit "To" EPR) --&gt;
                  &lt;send/&gt;
              &lt;/filter&gt;
          &lt;/in&gt;
          &lt;out&gt;
              &lt;send/&gt;
          &lt;/out&gt;
          &lt;description&gt;The main sequence for the message mediation&lt;/description&gt;
      &lt;/sequence&gt;
      &lt;sequence name="outDispatchSeq"&gt;
          &lt;log level="full"&gt;
              &lt;property name="MESSAGE" value="Executing outDispatchSeq -------------- "/&gt;
          &lt;/log&gt;
          &lt;respond/&gt;
      &lt;/sequence&gt;
      &lt;sequence name="dispatchSeq"&gt;
          &lt;payloadFactory media-type="json"&gt;
              &lt;format&gt;$1&lt;/format&gt;
              &lt;args&gt;
                  &lt;arg evaluator="xml" expression="get-property('body')"/&gt;
              &lt;/args&gt;
          &lt;/payloadFactory&gt;
          &lt;property expression="json-eval($.testPropertyName)"
              name="testPropertyName" scope="default" type="STRING"/&gt;
          &lt;log level="full"&gt;
              &lt;property name="MESSAGE" value="Executing dispatchSeq -------------- "/&gt;
          &lt;/log&gt;
      &lt;/sequence&gt;
      &lt;!-- You can add any flat sequences, endpoints, etc.. to this synapse.xml file if you do
      *not* want to keep the artifacts in several files --&gt;
      &lt;inboundEndpoint name="test" onError="fault" protocol="ws"
          sequence="dispatchSeq" suspend="false"&gt;
          &lt;parameters&gt;
              &lt;parameter name="inbound.ws.port"&gt;9091&lt;/parameter&gt;
              &lt;parameter name="ws.outflow.dispatch.sequence"&gt;outDispatchSeq&lt;/parameter&gt;
              &lt;parameter name="ws.client.side.broadcast.level"&gt;0&lt;/parameter&gt;
              &lt;parameter name="ws.outflow.dispatch.fault.sequence"&gt;fault&lt;/parameter&gt;
              &lt;parameter name="content.type"&gt;application/json&lt;/parameter&gt;
          &lt;/parameters&gt;
      &lt;/inboundEndpoint&gt;
  &lt;/definitions&gt;
  </code></pre>
  
  <p>Then I create simple JavaScript code to connect with WebSocket and send simple JSON data:</p>
  
  <pre><code>function connect() {
      webSocket = new WebSocket('ws://localhost:9091/');
  
  
      webSocket.onopen = function(e) {
          console.log('connected: ' + webSocket);
      };
  
      // some logic here...
      webSocket.send("{ \"testPropertyName\": \"testValue\"}");
  }
  </code></pre>
  
  <p>But by all the time, WSO2 cannot parse my JSON object. I am getting errors like:</p>
  
  <pre><code>DEBUG - PayloadFactoryMediator #mediate. Transformed payload format&gt;&gt;&gt;
  ERROR - SynapseJsonPath #stringValueOf. Error evaluating JSON Path &lt;$.testPropertyName&gt;. Returning empty result. Error&gt;&gt;&gt; invalid path
   INFO - LogMediator To: , MessageID: urn:uuid:93c8c0bc-2a77-4895-a852-1267e21f2044, Direction: request, MESSAGE = Executing dispatchSeq -------------- , Payload: {}
  DEBUG - PayloadFactoryMediator #mediate. Transformed payload format&gt;&gt;&gt;
  ERROR - SynapseJsonPath #stringValueOf. Error evaluating JSON Path &lt;$.testPropertyName&gt;. Returning empty result. Error&gt;&gt;&gt; invalid path
   INFO - LogMediator To: , MessageID: urn:uuid:828216c3-a3ba-476a-886d-302a62e117be, Direction: request, MESSAGE = Executing dispatchSeq -------------- , Payload: {}
  </code></pre>
  
  <hr>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>After changing loggin to:</p>
  
  <pre><code>log4j.logger.org.apache.synapse.transport.http.wire=DEBUG
  </code></pre>
  
  <p>and adding loggig just after <code>payloadFactory</code>:</p>
  
  <pre><code>&lt;sequence name="dispatchSeq"&gt;
      &lt;payloadFactory media-type="json"&gt;
          &lt;format&gt;$1&lt;/format&gt;
          &lt;args&gt;
              &lt;arg evaluator="xml" expression="get-property('body')" /&gt;
          &lt;/args&gt;
      &lt;/payloadFactory&gt;
      &lt;log level="full" /&gt;
      &lt;property expression="json-eval($.testPropertyName)" name="testPropertyName" scope="default" type="STRING" /&gt;
      &lt;log level="full"&gt;
          &lt;property name="MESSAGE" value="Executing dispatchSeq -------------- " /&gt;
      &lt;/log&gt;
  &lt;/sequence&gt;
  </code></pre>
  
  <p>WSO2 is logging following output:</p>
  
  <pre><code>DEBUG - PayloadFactoryMediator #mediate. Transformed payload format&gt;&gt;&gt;
   INFO - LogMediator To: , MessageID: urn:uuid:7a04df37-b3c0-47a0-9ea1-78df460aec0a, Direction: request, Payload: {}
  ERROR - SynapseJsonPath #stringValueOf. Error evaluating JSON Path &lt;$.testPropertyName&gt;. Returning empty result. Error&gt;&gt;&gt; invalid path
   INFO - LogMediator To: , MessageID: urn:uuid:7a04df37-b3c0-47a0-9ea1-78df460aec0a, Direction: request, MESSAGE = Executing dispatchSeq -------------- , Payload: {}
  DEBUG - PayloadFactoryMediator #mediate. Transformed payload format&gt;&gt;&gt;
   INFO - LogMediator To: , MessageID: urn:uuid:9ef7732c-b196-435e-a800-5d98d670b11a, Direction: request, Payload: {}
  ERROR - SynapseJsonPath #stringValueOf. Error evaluating JSON Path &lt;$.testPropertyName&gt;. Returning empty result. Error&gt;&gt;&gt; invalid path
   INFO - LogMediator To: , MessageID: urn:uuid:9ef7732c-b196-435e-a800-5d98d670b11a, Direction: request, MESSAGE = Executing dispatchSeq -------------- , Payload: {}
  </code></pre>
  
  <hr>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I removed <code>payloadFactory</code> like:</p>
  
  <pre><code>&lt;sequence name="dispatchSeq"&gt;
      &lt;log level="full" /&gt;
      &lt;property expression="json-eval($.testPropertyName)" name="testPropertyName" scope="default" type="STRING" /&gt;
      &lt;log level="full"&gt;
          &lt;property name="MESSAGE" value="Executing dispatchSeq -------------- " /&gt;
      &lt;/log&gt;
  &lt;/sequence&gt;
  </code></pre>
  
  <p>WSO2 logs show me:</p>
  
  <pre><code>INFO - LogMediator To: , MessageID: urn:uuid:db00d7aa-69c8-4de6-8140-91169183c3d6, Direction: request, Envelope: &lt;?xml version='1.0' encoding='utf-8'?&gt;&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"&gt;&lt;soapenv:Body/&gt;&lt;/soapenv:Envelope&gt;
  WARN - SynapseJsonPath Json Payload is empty.
  INFO - LogMediator To: , MessageID: urn:uuid:db00d7aa-69c8-4de6-8140-91169183c3d6, Direction: request, MESSAGE = Executing dispatchSeq -------------- , Envelope: &lt;?xml version='1.0' encoding='utf-8'?&gt;&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"&gt;&lt;soapenv:Body/&gt;&lt;/soapenv:Envelope&gt;
  INFO - LogMediator To: , MessageID: urn:uuid:19816ec6-c255-4edf-adae-cca83dbd2008, Direction: request, Envelope: &lt;?xml version='1.0' encoding='utf-8'?&gt;&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"&gt;&lt;soapenv:Body/&gt;&lt;/soapenv:Envelope&gt;
  WARN - SynapseJsonPath Json Payload is empty.
  INFO - LogMediator To: , MessageID: urn:uuid:19816ec6-c255-4edf-adae-cca83dbd2008, Direction: request, MESSAGE = Executing dispatchSeq -------------- , Envelope: &lt;?xml version='1.0' encoding='utf-8'?&gt;&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"&gt;&lt;soapenv:Body/&gt;&lt;/soapenv:Envelope&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Modified <code>dispatchSeq</code>:</p>
  
  <pre><code>&lt;sequence name="dispatchSeq"&gt;
      &lt;log level="full" /&gt;
      &lt;property expression="json-eval($.testPropertyName)" name="testPropertyName" scope="default" type="STRING" /&gt;
      &lt;log level="full"&gt;
          &lt;property name="MESSAGE" value="Executing dispatchSeq -------------- " /&gt;
          &lt;property expression="get-property('websocket.source.handshake.present')" name="websocket.source.handshake.present.value" scope="default" type="STRING"/&gt;
      &lt;/log&gt;
  &lt;/sequence&gt;
  </code></pre>
  
  <p>WSO2 output is:</p>
  
  <pre><code>INFO - LogMediator To: , MessageID: urn:uuid:5cc82e8d-6638-4cca-97f7-ac4f96877f85, Direction: request, Envelope: &lt;?xml version='1.0' encoding='utf-8'?&gt;&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"&gt;&lt;soapenv:Body/&gt;&lt;/soapenv:Envelope&gt;
  WARN - SynapseJsonPath Json Payload is empty.
  INFO - LogMediator To: , MessageID: urn:uuid:5cc82e8d-6638-4cca-97f7-ac4f96877f85, Direction: request, MESSAGE = Executing dispatchSeq -------------- , websocket.source.handshake.present.value = true, Envelope: &lt;?xml version='1.0' encoding='utf-8'?&gt;&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"&gt;&lt;soapenv:Body/&gt;&lt;/soapenv:Envelope&gt;
  INFO - LogMediator To: , MessageID: urn:uuid:81b0e2bd-4e79-48bd-b177-f49fd5664c0a, Direction: request, Envelope: &lt;?xml version='1.0' encoding='utf-8'?&gt;&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"&gt;&lt;soapenv:Body/&gt;&lt;/soapenv:Envelope&gt;
  WARN - SynapseJsonPath Json Payload is empty.
  INFO - LogMediator To: , MessageID: urn:uuid:81b0e2bd-4e79-48bd-b177-f49fd5664c0a, Direction: request, MESSAGE = Executing dispatchSeq -------------- , websocket.source.handshake.present.value = null, Envelope: &lt;?xml version='1.0' encoding='utf-8'?&gt;&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"&gt;&lt;soapenv:Body/&gt;&lt;/soapenv:Envelope&gt;
  </code></pre>
  
  <p>Do you know what else I am missing?
  I will appreciate any code examples.</p>
  
  <p>Thank you very much!</p>