Home > Mobile >  How do I check and return all same values from a JSON file?
How do I check and return all same values from a JSON file?

Time:09-30

I'm trying to make a function that checks a json file for all values that match the input, input being "property" = True

Basically I'm looking for a function to return item1 and item2

{"item1": {"property": True}, "item2": {"property": True}, "item3": {"property": False}}

CodePudding user response:

Simple comprehension will do the trick:

tuple(k for k in d if d[k]["property"])

CodePudding user response:

Use a dictionary comprehension with a condition.

data = {"item1": {"property": True}, "item2": {"property": True}, "item3": {"property": False}}

result = {key: value for key, value in data.items() if value['property']}
  • Related