0

Recently I created a SurveyMonkey API program that lets me create and upload surveys much faster for my purposes than using the website. My problem is that I switched up my program to make it more versatile and have run into a problem.

The part I am stuck on is when I use the program to insert image URLs into the JSON formatted upload payload.

I first create the payload, called page, and iterate through the image URLs list to upload, adding the URL to the specific section in the page along the way.

The problem that I am having is when I set a field within an entry of the page list, it changes that field for all of the page list entries.

For Example

page = final payload to upload to surveymonkey
page_template = list of JSON format questions for one page
num_pics = len(img_url_list)

for i in range(num_pics):
    # Try except for if page does not have questions
    try:
        for question_index in range(len(page_template)):
            if "presentation" in page_template[question_index]["family"]:
                page_template[question_index]["headings"][0]
                                    ["image"]["url"] = img_url_list[i]
    except:
        print("Problem when inserting images...")

    page[i] = {
        "title": " ",
        "description": " ",
        "questions": page_template

for one_page in page[:i+1]:
    for one_question in one_page["questions"]:
        if "presentation" in one_question["family"]:
            print(one_question["headings"][0]["image"]["url"])

This outputs something like...

img1url.jpg

img2url.jpg
img2url.jpg

img3url.jpg
img3url.jpg
img3url.jpg

I simplified the code or else it would take forever to explain this program to a further extent.

I cannot for the life of me figure out why the fields within the list entries are being changed for all entries instead of only the one I indexed to. I have tried changing around my code to use temporary variables and/or outside functions but nothing has worked.

Any thoughts?

EDIT

I am looking to have the output look like...

image1url.jpg

image1url.jpg
image2url.jpg

image1url.jpg
image2url.jpg
image3url.jpg

This would show that the same image URL is not being copied to all of the body page templates.

rjweld21
  • 3
  • 3
  • There seems to be a good amount of code in your question unrelated to your actually problem. Could you create a [mcve]? – Christian Dean Aug 29 '17 at 20:57
  • I'm not sure I understand everything your saying, but you're probably running into [this](https://stackoverflow.com/questions/19951816/python-changes-to-my-copy-variable-affect-the-original-variable) problem. – Christian Dean Aug 29 '17 at 20:59

2 Answers2

0

I'm not sure exactly your problem, or what the expected output is, but based on my understanding, I refactored your code a little to use enumerate and not use unnecessary index to make it a bit more readable.

for i, url in enumerate(img_url_list):

    # Try except for if page does not have questions
    try:
        for question in page_template:
            if "presentation" in question["family"]:
                question["headings"][0]["image"]["url"] = url
    except:
        print("Problem when inserting images...")

    page[i] = {
        "title": " ",
        "description": " ",
        "questions": page_template
    }

So what you're trying to do is create a new page with the same questions but different URLs for each? What's happening is that you are updating question by reference. So page[0]['questions'] and page[1]['questions'] is pointing to the same place in memory. One way to get around this is to make a copy of the page_template before each change:

from copy import deepcopy

for i, url in enumerate(img_url_list):
    # Make a copy of the page for this iteration
    current_page = deepcopy(page_template)

    # Try except for if page does not have questions
    try:
        for question in current_page:
            if "presentation" in question["family"]:
                question["headings"][0]["image"]["url"] = url
    except:
        print("Problem when inserting images...")

    page[i] = {
        "title": " ",
        "description": " ",
        "questions": current_page
    }

Hope that helps, otherwise I'll need a bit more info on expected output and maybe example page_template/img_url_list values.

General Kandalaft
  • 2,215
  • 2
  • 18
  • 25
0

Actually found the answer...

The thing was that I was pulling a template of the body page JSON format from a JSON file. This template was then copied once for each image URL there was.

I guess since I only read the JSON file once and copied the information, it was making all of the copies uniform.

To fix this I just wrote a function that accepts the JSON file path and image URL as inputs. The function then reads the JSON file for the body template and inserts the image URL. The output is the resulting body JSON format with the image URL inserted.

Since this function reads the JSON file once for every image it is inserting, it avoids the error I was having before.

rjweld21
  • 3
  • 3