Get value from ID JSON response surveymonkey API v3

戏子无情 提交于 2019-12-04 18:39:20

There isn't a direct payload returned from the API right now that has the answer text as well.

You'll need to fetch your survey details first:

SURVEY_DETAIL_ENDPOINT = "/v3/surveys/SURVEYID/details"
uri = "%s%s" % (HOST, SURVEY_DETAIL_ENDPOINT)

response = client.get(uri, headers=headers)

survey_data = response.json()

Then you likely want to loop through the answers to create a lookup dictionary. Roughly something like:

answer_dict = {}
for page in survey_data['pages']:
    for question in page['questions']:

        # Rows, choices, other, and cols all make up the possible answers
        answers = question['answers'].get('rows', [])\
            + question['answers'].get('choices', [])\
            + question['answers'].get('other', [])\
            + question['answers'].get('cols', [])

        for answer in answers:
            answer_dict[answer['id']] = answer

I know this looks rough with for loops but you're basically just traversing the entire survey. This works because choice IDs are globally unique (i.e. no collisions between columns, rows, choices etc.).

Then you can easily get the entire answer details by doing answer_dict[answer['choice_id']] for any answer in the response.

It wouldn't be a bad idea if the API itself allowed an option to fill in the answer text with the response for you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!