Bird
0
0

You want to read a JSON file and filter test data where the key active is true. Given data.json content:

hard📝 Application Q9 of 15
Selenium Python - Data-Driven Testing
You want to read a JSON file and filter test data where the key active is true. Given data.json content:
[{"id":1, "active": true}, {"id":2, "active": false}, {"id":3, "active": true}]

Which code correctly prints the list of active ids?
Awith open('data.json') as f: data = json.load(f) active_ids = [item['id'] for item in data if item['active']] print(active_ids)
Bwith open('data.json') as f: data = json.loads(f) active_ids = [item['id'] for item in data if item['active']] print(active_ids)
Cwith open('data.json') as f: data = json.load(f) active_ids = [item['id'] for item in data if item['active'] == 'true'] print(active_ids)
Dwith open('data.json') as f: data = json.load(f) active_ids = [item['id'] for item in data if item['active'] == 'True'] print(active_ids)
Step-by-Step Solution
Solution:
  1. Step 1: Load JSON array from file

    Use json.load(f) to read the JSON list of dictionaries.
  2. Step 2: Filter list comprehension by boolean key

    Since active is a boolean, filtering with if item['active'] works correctly.
  3. Final Answer:

    with open('data.json') as f: data = json.load(f) active_ids = [item['id'] for item in data if item['active']] print(active_ids) -> Option A
  4. Quick Check:

    Filter JSON list by boolean key using list comprehension [OK]
Quick Trick: Filter JSON list with if item['key'] for booleans [OK]
Common Mistakes:
  • Using json.loads() on file object
  • Comparing boolean to string 'true'
  • Using == True unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Python Quizzes