0

I'm creating a function (processElements) that uses a JSON object as variable (json_template). This function is in charge of generating an array of that JSON object (jsonElementArray), setting up the values according to what is received as function parameter (fruitsArray).

The problem is that when I'm calling the function, it's overwriting the parameter of the elements that make up the array. The parameter of all elements take the value of the parameter of the last element.

The mentioned function is the as follows:

def processElements(fruitsArray):
    jsonElementArray = []
    
    for fruit in fruitsArray:
        jsonElement = json_template
        jsonElement['name'] = fruit['id']
        jsonElementArray.append(jsonElement)
    
    return jsonElementArray

The JSON template that I use as variable in the function is as follows:

json_template = {
    "type": "fruit",
    "name": ""
}

A sample JSON with the array that the function could receive as input is as follows:

jsonSample = {
    "fruits": [
        {
            "id": "apple"
        },
        {
            "id": "banana"
        },
        {
            "id": "orange"
        }
    ]
}

The function is being used as follows:

def prueba(request):
    print(processElements(jsonSample["fruits"]))

The (not wanted) result is as follows:

{'type': 'fruit', 'name': 'orange'}{'type': 'fruit', 'name': 'orange'}{'type': 'fruit', 'name': 'orange'}

The expected result is as follows:

{'type': 'fruit', 'name': 'apple'}{'type': 'fruit', 'name': 'banana'}{'type': 'fruit', 'name': 'orange'}
0

2 Answers 2

1

DownloadPizza rightly pointed out what the issue is in their answer, I just wanted to add a few more things:

Names! Stick to PEP 8 when possible. Also, referring to the objects here as JSON might be making things more complicated than necessary.

I'm not sure if that's your real prueba function, but having a function which does nothing but call another one and print the result is likely superfluous.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for all the advices, I'll consider them in future occasions . Didn't know about the existence of PEP8 style either.
0

The problem is that json_template is an instance, and if you say

a = json_template

Its still the same instance. How about instead of a global variable json_template you create a function

def get_json_template():
  return { "type" : "fruit", "name" : ""}

Or you just inline the json template like

# snip
jsonElement = { "type" : "fruit", "name" : ""}
# snip

I think the 2nd one is more readable.

The most pythonic way would be a list comprehension however:

out = [{'type' : 'fruit', 'name' : fruit['id']} for fruit in fruits] 

TLDR use the last one

1 Comment

Thanks for the answer. I tried the three different ways to solve the problem you mentioned. I'm definetely sticking with the last one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.