There is a file db.json

Its content is:

 [ { "user": "981" }, { "user": "859" }, { "user": "237" } ] 

For example, you need to check if there is a "user": "859" in db.json

How do I do it:

 import json def json_read(file_name): try: json_data = json.load(open(file_name, 'r', encoding="cp1251")) except: json_data = [] return json_data data = json_read("db.json") user_in_db = "859" for user in data: if user_in_db == user['user']: print("yes") else: print("No") 

But I think in the case of a large number of users in db.json this method is less convenient. Is there any more convenient and faster way to do this?

    2 answers 2

    Solution to the forehead:

     In [13]: db = [ ...: { ...: "user": "981" ...: }, ...: { ...: "user": "859" ...: }, ...: { ...: "user": "237" ...: } ...: ] In [14]: {'user': '859'} in db Out[14]: True 

    Or:

     In [15]: any(x['user'] == '859' for x in db) Out[15]: True 
       data = json.load(open('db.json', 'r')) dictValueKey = dict([ [d.get(k),k] for k in d for d in data]) print("Yes" if dd.get("859")=="user" else "No") Yes print("Yes" if dd.get("111")=="user" else "No") No print("Yes" if dd.get("859")=="userNo" else "No") No