Bird
Raised Fist0

Why does json.dumps() with sort_keys=True sort keys inside nested dictionaries by default?

hard🧠 Conceptual Q10 of Q15
Python - Structured Data Files
Why does json.dumps() with sort_keys=True sort keys inside nested dictionaries by default?
ABecause nested dictionaries are converted to strings before sorting.
BBecause sorting keys is disabled for nested dictionaries by default.
CBecause <code>sort_keys=True</code> sorts keys recursively at all levels of the dictionary.
DBecause <code>json.dumps()</code> cannot handle nested dictionaries.
Step-by-Step Solution
Solution:
  1. Step 1: Understand sort_keys behavior

    The sort_keys=True parameter sorts keys alphabetically at every dictionary level, including nested ones, during serialization.
  2. Step 2: Differentiate from other options

    A is incorrect; nested dicts are recursively processed. C is wrong; sorting applies to nested. D is false; nested dicts are fully supported. B correctly states recursive sorting.
  3. Final Answer:

    Because sort_keys=True sorts keys recursively at all levels of the dictionary. -> Option C
  4. Quick Check:

    sort_keys=True sorts keys recursively [OK]
Quick Trick: sort_keys=True sorts keys at all levels recursively [OK]
Common Mistakes:
MISTAKES
  • Thinking sort_keys only sorts top-level keys
  • Believing nested dicts are not sorted
  • Assuming json.dumps can't handle nested dicts

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes