Bird
0
0

You want to save a Python dictionary data to a JSON file with indentation for readability. Which code is correct?

hard📝 Application Q9 of 15
Python - Structured Data Files

You want to save a Python dictionary data to a JSON file with indentation for readability. Which code is correct?

Awith open('output.json', 'w') as f: json.dumps(data, f, indent=4)
Bwith open('output.json', 'w') as f: json.dump(data, f, indent=4)
Cwith open('output.json', 'w') as f: json.load(data, f, indent=4)
Dwith open('output.json', 'w') as f: json.loads(data, f, indent=4)
Step-by-Step Solution
Solution:
  1. Step 1: Use json.dump() to write dict to file

    json.dump() writes JSON to file, accepts indent parameter for formatting.
  2. Step 2: Confirm correct function and parameters

    json.dumps() returns string, not for writing directly to file; load and loads are for reading.
  3. Final Answer:

    with open('output.json', 'w') as f: json.dump(data, f, indent=4) -> Option B
  4. Quick Check:

    Use json.dump() with indent to write pretty JSON [OK]
Quick Trick: Use json.dump() with indent to save readable JSON [OK]
Common Mistakes:
  • Using dumps() instead of dump()
  • Confusing load and dump functions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes