Reputation: 55
I am trying to get a Python variable based on a JSON response.
When I request the JSON I want to save some of the information as
X = content[0]['OrderItemList'][0]['ItemID']
What I need is the variable X
to change if it is a certain number.
For example, if x = 3
, I need it be changed to x = 5
. So far I have been able to change the variable with math and other string or integer changes. I think a loop would work but I am not sure.
if request.method == 'POST':
x = content[0]['OrderItemList'][0]['ItemID']
pseudocode
if x = 2 chnage to x = 5
Upvotes: 0
Views: 240
Reputation: 354
Simply set x to the value from json, then use if/elif
statements to reassign x.
x = content[0]['OrderItemList'][0]['ItemID']
if x in [2, 3]:
# if x is 2 or 3, set to 5
x = 5
elif x == 4:
x = 2
or if you have a lot of conditions on which to change x, consider using a dict :
changetos = {2: 5,
3: 5,
#...
}
x = content[0]['OrderItemList'][0]['ItemID']
x = changetos.get(x, x) # if x not in changetos, leave x as is
Upvotes: 1