How do I get question names using the REST API?

583
2
Jump to solution
10-18-2018 08:55 AM
MattStayner
Occasional Contributor II

I would like to get the question names for a Survey123 form using the REST API. What is the best way to do that?

I found the following endpoint, which gets me close:

https://www.arcgis.com/sharing/rest/content/items/[item_number]/info/[form_name].webform

In the return object from the above endpoint I get a key-value pair where the key is "model" and the "value" is shown below. It looks like XML, but there is a "\n" on every line that is making it difficult for us to process.

<Form_3            
xmlns:esri=\"http://esri.com/xforms\"            
xmlns:jr=\"http://openrosa.org/javarosa\"            
xmlns:odk=\"http://www.opendatakit.org/xforms\"            
xmlns:orx=\"http://openrosa.org/xforms\" id=\"Form_3\">\n                                  
<starttime/>\n                                  
<AOMInitials/>\n                                  
<AOMOther/>\n                                  
<DailyScheduled/>\n                                  
<SpecialInspectionType/>\n                                  
<SpecialOther/>\n                                  
<SpecialLocation/>\n             
<endtime/>\n                                  
<meta>\n                                            
<instanceID/>\n                                  
</meta>\n                        
</Form_3>\n‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

How can we convert that to standard XML/JSON? Is there a better endpoint we could use where we could get back the "model" in JSON or correctly formatted JSON?

Thanks!

Matt

 

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
JamesTedrick
Esri Esteemed Contributor

Hi Matt,

The webform uses an XForm XML file that has been embedded within a JSON file.  To get the field names directly, there are a couple of methods:

1) Scan the Feature Service Layer's REST endpoint.  The fields are present and, if published by Survey123, will have the labels of the questions set as field aliases.

2) Access the [form name].xml info resource (as opposed to the [form name].webform) - you will see this in the file folder of a survey in either Survey123 field app or Survey123 Connect.

View solution in original post

0 Kudos
2 Replies
JamesTedrick
Esri Esteemed Contributor

Hi Matt,

The webform uses an XForm XML file that has been embedded within a JSON file.  To get the field names directly, there are a couple of methods:

1) Scan the Feature Service Layer's REST endpoint.  The fields are present and, if published by Survey123, will have the labels of the questions set as field aliases.

2) Access the [form name].xml info resource (as opposed to the [form name].webform) - you will see this in the file folder of a survey in either Survey123 field app or Survey123 Connect.

0 Kudos
MattStayner
Occasional Contributor II

James,

Perfect! We opted for option 2 where we can hit the endpoint sharing/rest/content/items/SurveyID/info/[name].xml 

I knew the XML file existed in the folder on my laptop. I didn't realize that endpoint existed. That makes it way easy to access the form definition. Thanks!

0 Kudos