简体   繁体   中英

How to pass a JSON array with various elements to a http payload?

I am trying to develop WSO2 ESB connector for ToodleDo API v3. In that API, I need to add an array of tasks with various parameters. The number of parameters in one element of the array, can be different from other elements. The API call looks as below.

http://api.toodledo.com/3/tasks/add.php
    access_token=yourtoken
    tasks=[{"title"%3A"My Task"}%2C{"title"%3A"Another"%2C"star"%3A"1"%2C"ref"%3A"98765"}%2C{"title"%3A""%2C"ref"%3A"1234"}]
    fields=folder,star

Tasks are added by creating a JSON object and submitting a POST to the API. Also I need to encode the data before transferring via the URL.

I tried this operation by using javascript. I just tried with two parameters in the array (Mandatory -title and optional - star). My code is as below.

    var added = 0;
    var query = "";

    var taskString = '{"tasks":' + mc.getProperty('uri.var.tasks') + '}';

   if (mc.getProperty('uri.var.tasks') != null && mc.getProperty('uri.var.tasks') != "") {

        var taskObj = eval ("(" + taskString + ")");

        if(Boolean(added)) {
            query = query + ',"tasks":[';
        }else {
            query = query + '"tasks":[';
            added = 1;
        }

        var count = 0;
        for (var i in taskObj.tasks) {
            if (taskObj.tasks.hasOwnProperty(i)) {
                if(count == 0 ) {
                    query=query + '{\"title\":\"'+mc.getProperty('uri.var.title')+'\",';
                    query=query + '{\"star\":\"'+mc.getProperty('uri.var.star')+'\"}';
                    count =1;
                }else {
                    query=query + ',{\"title\":\"'+mc.getProperty('uri.var.title')+'\",';
                    query=query + '{\"star\":\"'+mc.getProperty('uri.var.star')+'\"}';
                }

            }
        }

        query = query + ']';

        var encoded_query= encodeURIComponent(query);
    }
    mc.setProperty('uri.var.encoded_query', encoded_query);

]]>
</script>

And I have written the payload factory as below.

<payloadFactory media-type="xml">
            <format>
                <xform>
                    <access_token>$1</access_token>
                    <tasks>$2</tasks>
                    <fields>$3</fields>
                </xform>
                <!--{
                    "access_token" : "$1",
                    "tasks" : "$2"
                    "fields" : "$3"
                }-->
            </format>
            <args>
                <arg expression="$func:access_token" />
                <arg expression="get-property('uri.var.encoded_query')" />
                <arg expression="$func:fields" />
            </args>
</payloadFactory>

But it gives me the following error.

[2014-09-10 22:39:22,129] ERROR - SynapseJsonPath #stringValueOf. Error evaluating JSON Path <$.access_token>. Returning empty result. Error>>> Unexpected character ({) at position 96.
[2014-09-10 22:39:22,130] ERROR - SynapseJsonPath #stringValueOf. Error evaluating JSON Path <$.tasks>. Returning empty result. Error>>> Unexpected character ({) at position 96.
[2014-09-10 22:39:22,130] ERROR - SynapseJsonPath #stringValueOf. Error evaluating JSON Path <$.fields>. Returning empty result. Error>>> Unexpected character ({) at position 96.
[2014-09-10 22:39:22,132] ERROR - ScriptMediator Failed to get the JSON payload from the input stream. Error>>>
com.google.gson.stream.MalformedJsonException: Expected name at line 1 column 101

What am I missing? How can I modify this to work with all the optional parameters?

You can add the required fields in a dictionary and then call JSON.stringify on that dictionary to get the desired JSON object.

var field = {};
field['key'] = 'value';

Then using Ajax Query you can sned the data in a payload.

$.ajax({
        type : "POST",
        url : url,
        data : JSON.stringify(field),
        dataType : "json",
        success : alert('success'),
        error: alert('error')
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM