Challenge - 5 Problems
Structured Data Formatter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of JSON formatting with indent
What is the output of the following Python code that formats a dictionary as a JSON string with indentation?
Python
import json data = {'name': 'Alice', 'age': 30, 'city': 'New York'} result = json.dumps(data, indent=2) print(result)
Attempts:
2 left
💡 Hint
Look at how json.dumps formats the output when indent=2 is used.
✗ Incorrect
Using json.dumps with indent=2 formats the JSON string with two spaces before each key-value pair inside the braces, producing a nicely indented output.
❓ Predict Output
intermediate2:00remaining
Output of pretty-printing nested data
What is the output of this code that pretty-prints a nested dictionary using json.dumps with indent=4?
Python
import json nested = {'user': {'name': 'Bob', 'details': {'age': 25, 'city': 'Paris'}}} print(json.dumps(nested, indent=4))
Attempts:
2 left
💡 Hint
Indent=4 means four spaces per level of nesting.
✗ Incorrect
json.dumps with indent=4 adds four spaces for each nested level, so the output is deeply indented accordingly.
❓ Predict Output
advanced2:00remaining
Output of formatting a list of dictionaries with separators
What is the output of this code that formats a list of dictionaries as JSON with custom separators?
Python
import json items = [{'id': 1, 'value': 10}, {'id': 2, 'value': 20}] print(json.dumps(items, separators=(',', ':')))
Attempts:
2 left
💡 Hint
Separators=(',', ':') remove spaces after commas and colons.
✗ Incorrect
The separators argument removes spaces after commas and colons, producing a compact JSON string without spaces.
❓ Predict Output
advanced2:00remaining
Output of sorting keys in JSON output
What is the output of this code that formats a dictionary as JSON with sorted keys?
Python
import json data = {'b': 2, 'a': 1, 'c': 3} print(json.dumps(data, sort_keys=True))
Attempts:
2 left
💡 Hint
sort_keys=True orders keys alphabetically.
✗ Incorrect
When sort_keys=True is used, keys are output in alphabetical order with spaces after colons by default.
🧠 Conceptual
expert2:00remaining
Effect of ensure_ascii=False in JSON formatting
Which option correctly describes the effect of using ensure_ascii=False in json.dumps when formatting data containing non-ASCII characters?
Attempts:
2 left
💡 Hint
Try printing json.dumps with and without ensure_ascii=False on data with accented letters.
✗ Incorrect
By default, json.dumps escapes non-ASCII characters as Unicode sequences. Using ensure_ascii=False keeps the original characters in the output string.