Pretty-Print Config as JSON

Easy ⏱ 8 min 75% acceptance ★★★★☆ 4.3
Write pretty(config) serializing a dict to a JSON string with 2-space indentation and sorted keysjson.dumps(..., indent=2, sort_keys=True) — so diffs of config files stay stable.

Examples

Example 1
Input
pretty({'b': 1, 'a': 2})
Output
a JSON string with "a" before "b", indented 2 spaces
Explanation

Keys are alphabetized and indented.

Constraints

  • indent=2, sort_keys=True.
  • Return the string, do not print.

Topics

JSONdumps options

Companies

AtlassianGitLabPostman

Hints

Hint 1

Both are keyword arguments to dumps.

Hint 2

Sorted keys make output deterministic.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.