Bird
0
0

You have a nested dictionary:

hard📝 Application Q8 of 15
Python - Structured Data Files
You have a nested dictionary:
data = {'person': {'name': 'Eve', 'age': 28}, 'city': 'Paris'}

Which code correctly formats this data as pretty JSON with 4 spaces indentation and sorted keys at all levels?
Ajson.dumps(data, indent=4, sort_keys=True)
Bjson.dumps(data, indent=4, sort_keys=False)
Cjson.dumps(data, indent='4', sort_keys=True)
Djson.dumps(data, indent=2, sort_keys=True)
Step-by-Step Solution
Solution:
  1. Step 1: Choose correct indent and sort_keys values

    To get pretty JSON with 4 spaces indentation, use indent=4 as integer. To sort keys at all levels, use sort_keys=True.
  2. Step 2: Exclude incorrect options

    json.dumps(data, indent=4, sort_keys=False) disables sorting keys. json.dumps(data, indent='4', sort_keys=True) uses string '4' for indent, which is invalid. json.dumps(data, indent=2, sort_keys=True) uses 2 spaces indent, not 4.
  3. Final Answer:

    json.dumps(data, indent=4, sort_keys=True) -> Option A
  4. Quick Check:

    indent=4 and sort_keys=True for pretty sorted JSON [OK]
Quick Trick: Use indent=4 and sort_keys=True for pretty sorted JSON [OK]
Common Mistakes:
  • Passing indent as string
  • Not sorting keys when needed
  • Using wrong indentation level

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes