0

Javascript execute and batchable inputs

Hi,

I'm implementing a YQL table with an "execute" statement to query a web service using Javascript. I have an input parameter which I need to be batchable, so that I can pass through multiple values to the web service.

For example:

YQL: SELECT * FROM teams WHERE teamId IN (1, 2, 3, 4)

calls this URL:

http://myserver/service/teams?teamIds=1,2,3,4

In my "execute" Javascript block, when I try to access the input variable (i.e. inputs['teamIds'] ) the object doesn't seem to be an array (it doesn't support any Javascript array methods like "join()" or "length"). It only seems to work for "toString()" which outputs text in the format "[1, 2, 3, 4]".

How would I access the individual items in a batchable input variable? I would like to do something like this:

var teamString = inputs['teamId'].join("|");

Thanks, Nathan

by
2 Replies
  • instead on you query being in try query like this

    select * from testteam where teamId = "1,2,3,4,5,2,4"

    In you execute block read the teamId , parse into an array and you are all set. You have to make an array object to get access of join and other array specific property!

    Here is my Execute Code block

    function parseJSON(string){
      return eval('(' string ')');
    }
    var teamId = unescape(teamId);
    var teams= parseJSON("[" teamId "]");
    //force make it array if it is not
    if(!teams){
      teams=[];
    }
    if(teams[0]==undefined){
    teams=[teams];
    }
    //now teams is a array do what oever u want
    response.object = {"teams":teams,"teamsJoin":teams.join('|')};
    

    This is My Table output

    <?xml version="1.0" encoding="UTF-8"?>
    <query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng"
        yahoo:count="1" yahoo:created="2013-02-01T07:57:29Z" yahoo:lang="en-US">
        <diagnostics>
            <publiclyCallable>true</publiclyCallable>
            <url execution-start-time="1" execution-stop-time="2"
                execution-time="1" proxy="DEFAULT"><![CDATA[store://JQ7iotYcBpZgxQd4h78Jij]]></url>
            <cache execution-start-time="4" execution-stop-time="4"
                execution-time="0" method="GET" type="MEMCACHED"><![CDATA[a2b12d28c86e55c34cad8548d94603b8]]></cache>
            <cache execution-start-time="4" execution-stop-time="4"
                execution-time="0" method="GET" type="MEMCACHED"><![CDATA[203.83.248.32]]></cache>
            <javascript execution-time="1" instructions-used="0" table-name="testteam"/>
            <user-time>5</user-time>
            <service-time>1</service-time>
            <build-version>33614</build-version>
        </diagnostics> 
        <results>
            <result>
                <teams>1.0</teams>
                <teams>2.0</teams>
                <teams>3.0</teams>
                <teams>4.0</teams>
                <teams>5.0</teams>
                <teams>2.0</teams>
                <teams>4.0</teams>
                <teamsJoin>1|2|3|4|5|2|4</teamsJoin>
            </result>
        </results>
    </query>
    
    1
  • Thanks for the suggestion.

    I also discovered a solution using the y.parseJSON() method similar to your advice to make the parameter a Javascript Array object:

    Example YQL: select * from testteam where teamId IN (1,2,3,4,5,2,4)

    Javascript Code:

    var teamId = unescape(teamId);
    var teamIds = [teamId];
    if (teamId.indexOf("[") > -1) {
      teamIds = y.parseJSON(teamId);
    }
    
    0

Recent Posts

in YQL