python - Use IF statement to search json file -


i need search json file , print peole y status:

[[{"name": "person1", "status": "y"}], [{"name": "person2", "status": "n"}], [{"name": "person3", "status": "y"}]] 

i can open file , display data ok, need search within it. complete if statement me? needs @ keyvalue? status , print 2 people y

for name in openfile:                     **# if status == y  do here? :**                         print ("name : " +name[i]['name'])                         print ("status : " +name[i]['status']) 

you need iterate on lists, extract dict , check key 'status':

for element in openfile:     ele = element[0]  # grab dict     if ele.get('status') == 'y':         print 'name: {}'.format(ele.get('name')         print 'status: {}'.format(ele.get('status') 

your openfile object list of lists, inside it's dict structure. so, want address first object in list (that's dict) , perform check. ele.get()


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -