mrh042
mrh042

Reputation: 47

Mulesoft: Using MEL to access an array in JSON payload

I’m trying to call a stored procedure on a MySQL DB, but my MEL expression is not working. I’ve referenced - https://docs.mulesoft.com/mule-user-guide/v/3.7/json-module-reference

My setup
**********************************************************************
* Mule Runtime and Integration Platform                              *
* Version: 3.8.0 Build: 9461215f                                     *
* MuleSoft, Inc.                                                     *
* For more information go to                                         *
* http://www.mulesoft.com/mule-esb-enterprise                        *
*                                                                    *
* Server started: 8/31/16 11:23 AM                                   *
* JDK: 1.8.0_91 (mixed mode)                                         *
* OS: Mac OS X (10.11.6, x86_64)                                     *
* Host: AMAC02PF791G3QC (10.13.164.24)                               *
**********************************************************************

The Error

Message               : Execution of the expression "message.payload.lines[0].SKU" failed. (org.mule.api.expression.ExpressionRuntimeException).
Payload               : {
                          "po_id": 1234,
                          "lines": [
                            {
                              "SKU": "1000",
                              "Primary SSA": "990471-Franjo",
                              "pre_pro": 1,
                              "pre_pro_need_by": "01/01/2016",
                              "top": 0,
                              "top_need_by": "01/01/2016",
                              "sample_size:": "small"
                            },
                            {
                              "SKU": "1000",
                              "Primary SSA": "990471-Franjo",
                              "pre_pro": 1,
                              "pre_pro_need_by": "01/01/2016",
                              "top": 0,
                              "top_need_by": "01/01/2016",
                              "sample_size:": "small"
                            }
                          ]
                        }
Payload Type          : java.lang.String
Element               : /post:\/samples:application\/json:api-config/processors/0 @ r-exp-1.3:api.xml:193
Element XML           : <db:stored-procedure config-ref="MySQL_Configuration" doc:name="Create Samples">
                        <db:parameterized-query>call  P_CREATE_SAMPLE_WRAP (@requestID,:primarySsa,:purchaseOrderId,:sku1,:sampleSize,:sampleColors,:createdBy,:lastUpdatedBy,:preProNeedBy,:preProType,:topNeedBy,:topType);</db:parameterized-query>
                        <db:in-param name="primarySsa" type="VARCHAR" value="'990471'"></db:in-param>
                        <db:in-param name="purchaseOrderId" type="INTEGER" value="#[message.payload.po_id]"></db:in-param>
                        <db:in-param name="sku1" type="INTEGER" value="#[message.payload.lines[0].SKU]"></db:in-param>
                        <db:in-param name="sampleSize" type="VARCHAR" value="#[message.payload.lines[0].sample_size]"></db:in-param>
                        <db:in-param name="sampleColors" type="VARCHAR" value="null"></db:in-param>
                        <db:in-param name="lastUpdatedBy" type="VARCHAR" value="null"></db:in-param>
                        <db:in-param name="createdBy" type="VARCHAR" value="null"></db:in-param>
                        <db:in-param name="preProNeedBy" type="DATE" value="#[message.payload.lines[0].pre_pro_need_by]"></db:in-param>
                        <db:in-param name="preProType" type="VARCHAR" value="null"></db:in-param>
                        <db:in-param name="topNeedBy" type="DATE" value="#[message.payload.lines[0].top_need_by]"></db:in-param>
                        <db:in-param name="topType" type="VARCHAR" value="null"></db:in-param>
                        <db:out-param name="requestID" type="INTEGER"></db:out-param>
                        </db:stored-procedure>I’ve also tried this expression with the same payload

I’ve also tried

Message               : Execution of the expression "message.payload.lines[0]/SKU" failed. (org.mule.api.expression.ExpressionRuntimeException).

I’ve only been using mulesoft for 2 weeks so any advice is greatly appreciated. Thanks!

Upvotes: 1

Views: 2139

Answers (3)

Vikalp
Vikalp

Reputation: 24

JSON is not supported by MEL. to access any field of Json, you can use below expression:

[dw('message.payload.lines[0].SKU')]

Upvotes: 0

Swati Vatyani
Swati Vatyani

Reputation: 47

your payload is of type String. Convert it to json or java object and then you can access it using the same way.

Upvotes: 1

sulthony h
sulthony h

Reputation: 1277

Error occurs because the payload is in form of String, and it fails executing message.payload.lines[0]/SKU.

Because the message format is a JSON, then you can try to replace it with json:lines[0]/SKU

Upvotes: 1

Related Questions