Using JSON and JSONP

For browser applications that want to retrieve poll and survey forms and results as JSON and JSONP, the following tags are available for use in Form and Results Style Templates, in conjunction with the Web Services API commands getpoll and getresults.

The following tags are available for both kinds of styles:

[/iffirst][/endiffirst]
[/iffirst,not][/endiffirst]

[/iflastt][/endiflast]
[/iflastt,not][/endiflast]

[/qindex]
[/aindex]

The iffirst and iflast tags are used between allquestionst and allanswers tags. They express everything between them for the first instance or the last instance, respectively. If used with ",not" then they express everything for all instances except the first or last instance. These tags express only those questions that are being Tallied according to the Tally page.

To use iffirst, endiffirst, iflast, and iendiflast in a Response style, be sure to enclose them in between {/ and ], rather than the uusual [/ and ], when used within the [/allanswers] tags. The examples below illustrate this. This allows using Other values in questions, such as writeins, and the iffirst and iflast tags need to be interpreted based on the values that are actually there, which requires that they be evaluated after the values are known. The {[ and ] tags ensure that they are interpreted at the proper time.

The qindex and aindex tags express as the ordinal number of the question or the answer between the allquestions and allanswers tags, starting with 0.

Another tag, used only in Response Style Templates, is useful for setting response headers:
[/responseheader]Accepting:JSONP[/endresponseheader]

Another tag is used only in Reponse Style Templates:
[/requestheader,name] expresses as the content of the HTTP request's header "name".

The contenttype tag can be used in both Form and Response Style Templates to set the Content-Type header:
[/contenttype,Application/JSON]
[/contenttype,text/plain]

Sample Form Style JSONForm to use with cmd=getpoll&style=JSONForm to get a voting form's elements expressed as JSON:

[/contenttype,application/json]
{
"polltitle":"[/polltitle]",
"pollid":"[/pollname]",
"spot":"[/spotname]",
"header":"[/pollheader]",
"footer":"[/pollfooter]",
"questions":[[/allquestionst]
{
"name":"[/qname]",
"type":"[/qtype]",
"text":"[/qtext]",
"help":"[/qhelp]",
"required":"[/questionmustbeanswered]",
"number":"[/qindex]",
"answers":[[/allanswers]
{
"value":"[/avalue]",
"text":"[/atext]",
"default":"[/checked]",
"skipto":"[/qskipto]",
"number":[/aindex]
}[/iflast,not],[/endiflast][/endallanswers]
]
}[/iflast,not],[/endiflast][/endallquestionst]
]
}

Note the contenttype to use may vary depending on your application and the browsers it supports.

Sample Response Style JSONResults to use with cmd=geresults&style=JSONResults to get live voting results expressed as JSON:

[/contenttype,application/json]
{
"polltitle":"[/reporttitle]",
"pollid":"[/pollname]",
"votes":"[/pollcount]",
"spot":"[/spotname]",
"header":"[/reportheader]",
"footer":"[/reportfooter]",
"questions":[[/allquestionst]
{
"name":"[/qnamex]",
"text":"[/qtextx]",
"answers":[[/allanswers]
{
"value":"[/avaluex]",
"text":"[/atextx]",
"count":[/^,count,*],
"percent":[/^,pctq,*]
"number":[/aindex]
}{/iflast,not],{/endiflast][/endallanswers]]
}[/iflast,not],[/endiflast][/endallquestionst]
]
}

Sample Response Style JSONPResults to use with cmd=geresults&style=JSONPResults to get live voting results expressed as JSON in a callback with an appropriate Accept header:

[/contenttype,application/javascript]
[/responseheader]Accepting:[/requestheader,Origin][/endresponseheader]
[/responseheader]Accepting:https://www.yahoo.com[/endresponseheader]
your_callback({
"pollid":"[/pollname]",
"votes":"[/pollcount]",
"spot":"[/spotname]",
"questions":{[/allquestionst]
"[/qnamex]":{[/allanswers]
"[/avaluex]":{"count":[/^,count,*],"percent":[/^,pctq,*]}[/iflast,not],[/endiflast][/endallanswers]
}{/iflast,not],{/endiflast][/endallquestionst]
}
});

Again, the contenttype to use may vary depending on your application and the browsers it supports.

Use only one of the two forms of the responseheader ... endresponseheader tag. The first form uses the [/requestheader,headername] tag which expresses as the content of the HTTP request's Origin header. The second form hard codes https://www.yahoo.com as the only acceptable origin.

 

Next: Styles How to