How can i connect JSON true or false to my python script

Question:

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 protected]",
    "password": "Password2*",
    "payment": "Paypal",
    "product": "https://www.unkown.de/de/product/",
    "Dummy":"False"
  },
  {
    "profilename": "Test1",
    "email": "[email protected]",
    "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?

Asked By: Yves1234

||

Answers:

I am not sure if I understand what your actual problem is. But here are some comments to the code you shared.

  • The intendation of your last print is off.
  • 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.
  • In the if you are not using ==
  • you are overwriting the 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')
Answered By: user_na
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.