Hi,

I am calling an endpoint which requires sending numeric keys in objects in
the following format:
{"1" : "some value"}

However, when processing this using a script mediator, the numeric key is
converted in to a primitive number as follows:
{1 : "some value"}

which is not accepted by the script mediator when setting the payload using
setPayloadJSON. 

This problem can also be seen when sending numeric values (not keys).
Although the values are numeric, the backend does not accept these values if
they are not sent as strings. e.g. {"key":"123"} is converted to {"key":123}
which is not accepted.


I managed to circumvent this problem by processing the payload via XML:

payloadXML = mc.getPayloadXML();
                                        var customFields = 
mc.getProperty("uri.var.customFields");
                                        
                                        var jsonObj = 
eval("("+customFields+")");
                                        var customFieldsTag = <cf/>;
                                        for(var key in  jsonObj){
                                                var val = jsonObj[key];
                                                customFieldsTag.cf +=
<_JsonReader_PD_{key}>{val}</_JsonReader_PD_{key}>;
                                }
                                        
                                        payloadXML.cf = customFieldsTag;
                                
                                        mc.setPayloadXML(payloadXML);

However, when processing the payload via XML, when there is only one element
within a JSON array, e.g. {"jsonaray" : ["some value"]}

this is processed as a single value (not within an array) and sent by the
XML processor as {"jsonarray" : "some value"}

Furthermore, when sending an empty value within a JSON array (which is
required by the backend) and processing using XML, e.g. {"jsonarray" :
["some value", ""]}, the empty value is converted to *null*, which is not
accepted by the backend. 


I would appreciate if someone could give me a solution to either;
* Send numeric keys and values as strings within a JSON payload without them
being processed in to numeric primitives.
* Send a single value within a JSON array without it being reprocessed as a
single key value pair by the XML-to-JSON processor. 


Thanks,




--
View this message in context: 
http://wso2-oxygen-tank.10903.n7.nabble.com/ESB-Problems-sending-numeric-keys-in-JSON-Payloads-tp99789.html
Sent from the WSO2 Development mailing list archive at Nabble.com.
_______________________________________________
Dev mailing list
Dev@wso2.org
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to