Bird
0
0

You have a JSON file users.json with multiple user records:

hard📝 Application Q8 of 15
Selenium Python - Data-Driven Testing
You have a JSON file users.json with multiple user records:
[{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]

How do you read this file and print the name of the second user?
Awith open('users.json') as f: users = json.loads(f) print(users[1]['name'])
Bwith open('users.json') as f: users = json.load(f) print(users[1]['name'])
Cwith open('users.json') as f: users = json.load(f) print(users['1']['name'])
Dwith open('users.json') as f: users = json.load(f) print(users[2]['name'])
Step-by-Step Solution
Solution:
  1. Step 1: Load JSON array from file

    The JSON file contains a list of dictionaries, so json.load(f) returns a list.
  2. Step 2: Access second element by index 1 and get 'name'

    Lists are zero-indexed, so the second user is at index 1. Access users[1]['name'] to get "Bob".
  3. Final Answer:

    with open('users.json') as f: users = json.load(f) print(users[1]['name']) -> Option B
  4. Quick Check:

    List index 1 accesses second user in JSON array [OK]
Quick Trick: JSON arrays load as lists; use zero-based indexes [OK]
Common Mistakes:
  • Using json.loads() on file object
  • Using string index '1' instead of integer
  • Accessing index 2 which is out of range

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Python Quizzes