Hello i am using python and want to connect JSON True or False to my code, so if "dummy": "True" do... else: do... My JSON
[
{
"profilename": "Test3",
"email": "email@outlook.com",
"password": "Password2*",
"payment": "Paypal",
"product": "https://www.unkown.de/de/product/",
"Dummy":"False"
},
{
"profilename": "Test1",
"email": "email@gmail.com",
"password": "Password1*",
"payment": "Paypal",
"product": "https://www.unkown.de/de/product/",
"Dummy":"True"
}
]
My python code:
profile_name = "Test3"
with open('Data2.json', 'r') as handle:
json = json.load(handle)
data = [x for x in json if x['profilename'] in profile_name]
email = (data[0]['email'])
password = (data[0]['password'])
payment = (data[0]['payment'])
product = (data[0]['product'])
dummy = (data[0]['dummy'])
def getLogin():
if dummy = "True": <----- This doesnt work, want to get this fixed
import Script2
else:
print("starting login process")
So like if the "Dummy": "True" it should to the if condition and if "dummy": false it should do the else condition, how can i do it?
I am not sure if I understand what your actual problem is. But here are some comments to the code you shared.
import Script2
is probably not what you want. If there is a function in Script2
import it at the top. Then just call the function.if
you are not using ==
json
module with a variable called json
Here is a reworked version
import Script2
import json
with open('Data2.json', 'r') as handle:
json_out = json.load(handle)
profile_name = ['Test1']
data = [x for x in json_out if x['profilename'] in profile_name]
email = (data[0]['email'])
password = (data[0]['password'])
payment = (data[0]['payment'])
product = (data[0]['product'])
dummy = (data[0]['Dummy']=='True')
def getLogin():
if dummy:
Script2.myfunction()
else:
print("starting login process")
An additional minor improvent would be to replace the list comprehension with a generator comprehension, as you don`t want a list anyways:
data = next(x for x in json_out if x['profilename'] in profile_name)
email = (data['email'])
password = (data['password'])
payment = (data['payment'])
product = (data['product'])
dummy = (data['Dummy']=='True')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.