We format structured data to make it easy to read and understand. It helps us share data clearly with others or save it in a neat way.
Formatting structured data in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
import json json.dumps(data, indent=number_of_spaces) # or print(json.dumps(data, indent=4))
json.dumps() converts Python data into a formatted string.
The indent parameter adds spaces to make the output easier to read.
Examples
Python
import json my_data = {'name': 'Alice', 'age': 30} print(json.dumps(my_data))
Python
import json my_data = {'name': 'Alice', 'age': 30} print(json.dumps(my_data, indent=2))
Python
import json my_list = [1, 2, 3, {'a': 4, 'b': 5}] print(json.dumps(my_list, indent=4))
Sample Program
This program formats a dictionary with nested list into a nicely indented JSON string and prints it.
Python
import json person = { 'name': 'John', 'age': 25, 'hobbies': ['reading', 'gaming', 'hiking'] } formatted = json.dumps(person, indent=4) print(formatted)
Important Notes
Use json.dumps() for formatting data as a string.
You can also use json.dump() to write formatted data directly to a file.
Indentation helps humans read data but is ignored by computers when parsing.
Summary
Formatting structured data makes it easier to read and share.
Use json.dumps() with indent to add spaces and new lines.
This is useful for printing, saving, or sending data clearly.
Practice
1. What does the
indent parameter do in json.dumps() when formatting structured data?easy
Solution
Step 1: Understand the purpose of
This function converts Python data into a JSON string.json.dumps()Step 2: Role of
Theindentparameterindentparameter adds spaces and new lines to format the JSON string nicely for readability.Final Answer:
Adds spaces and new lines to make the output easier to read -> Option AQuick Check:
indent= readable JSON [OK]
Hint: Remember: indent means pretty print with spaces [OK]
Common Mistakes:
- Thinking indent compresses JSON
- Confusing indent with encryption
- Assuming indent changes data content
2. Which of the following is the correct syntax to format a Python dictionary
data as a JSON string with indentation of 4 spaces?easy
Solution
Step 1: Check the correct parameter type for indent
Theindentparameter expects an integer number of spaces, not a string or boolean.Step 2: Validate each option
json.dumps(data, indent=4) usesindent=4correctly. json.dumps(data, indent='4') uses a string '4' which is invalid. json.dumps(data, indent=True) uses boolean True which is invalid. json.dumps(data, space=4) uses a wrong parameter namespace.Final Answer:
json.dumps(data, indent=4) -> Option AQuick Check:
Indent value must be integer [OK]
Hint: Use integer for indent, not string or boolean [OK]
Common Mistakes:
- Passing indent as a string instead of integer
- Using wrong parameter name like 'space'
- Passing boolean instead of number
3. What is the output of this code?
import json
data = {'name': 'Alice', 'age': 30}
print(json.dumps(data, indent=2))medium
Solution
Step 1: Understand json.dumps with indent=2
The function converts the dictionary to a JSON string with 2 spaces indentation for each nested level.Step 2: Check the output format
The output will have new lines and spaces, keys and string values in double quotes, and numeric values as is.Final Answer:
{ "name": "Alice", "age": 30 } -> Option BQuick Check:
Indent=2 adds spaces and new lines [OK]
Hint: Indent adds new lines and spaces for readability [OK]
Common Mistakes:
- Expecting compact JSON without spaces
- Using single quotes instead of double quotes
- Confusing Python dict print with JSON string
4. The following code throws an error. What is the mistake?
import json
data = {'x': 1, 'y': 2}
print(json.dumps(data, indent=2.0))medium
Solution
Step 1: Identify the error cause
Theindentparameter is given as a float 2.0 instead of an integer 2.Step 2: Understand parameter type requirements
json.dumps expects indent to be an integer number of spaces for formatting, passing a float causes a TypeError.Final Answer:
The indent parameter should be an integer, not a float -> Option DQuick Check:
Indent must be int, not float [OK]
Hint: Check indent type: must be integer, not float [OK]
Common Mistakes:
- Passing indent as float instead of int
- Thinking keys must be strings for json.dumps
- Expecting separators argument is mandatory
5. You have a list of dictionaries representing users:
How do you create a JSON string with indentation of 2 spaces and keys sorted alphabetically?
users = [{'name': 'Bob', 'age': 25}, {'name': 'Eve', 'age': 28}]How do you create a JSON string with indentation of 2 spaces and keys sorted alphabetically?
hard
Solution
Step 1: Use json.dumps with indent and sort_keys
Theindentparameter formats the JSON with spaces and new lines. Thesort_keys=Truesorts dictionary keys alphabetically.Step 2: Validate correct parameter names and types
json.dumps(users, indent=2, sort_keys=True) uses correct parameters and types. json.dumps(users, indent=2, sort=True) uses invalid parametersort. json.dumps(users, indent='2', sort_keys=True) passes indent as string which is invalid. json.dumps(users, pretty=2, sort_keys=True) uses invalid parameterpretty.Final Answer:
json.dumps(users, indent=2, sort_keys=True) -> Option CQuick Check:
Use indent=int and sort_keys=True [OK]
Hint: Use sort_keys=True and indent as integer [OK]
Common Mistakes:
- Using wrong parameter names like sort or pretty
- Passing indent as string
- Forgetting sort_keys to sort keys
