demographic_summary produces a nested dict — {"Junior": {"count": 42, "avg_satisfaction": 3.87}}. Your advisor wants to paste it into their notes app, email it, or load it into a reporting tool. How do you hand it over?
load_responses_from_csv from yesterday gets me to the dict. But a Python dict is not something I can paste into an email or store as a file.
json.dumps converts a Python dict to a JSON-formatted string. indent=2 makes it readable — nested structure, two-space indentation, the way every API response and config file looks. json.loads goes the other way, string back to dict:
import json
data = {"Junior": {"count": 42, "avg_satisfaction": 3.87}}
json_str = json.dumps(data, indent=2)
print(json_str)
# {\n "Junior": {\n "count": 42,\n "avg_satisfaction": 3.87\n }\n}Why not just print the dict directly? It already shows the values.
A printed dict uses Python syntax — single quotes, True/False capitalised, no trailing commas. JSON uses double quotes and true/false. They look similar but any JSON parser, API, or reporting tool expects the JSON format, not Python syntax:
def responses_to_json(responses: list) -> str:
"""Serialise the demographic summary to a JSON string."""
summary = demographic_summary(responses, "year")
json_str = json.dumps(summary, indent=2)
print(f"JSON output ({len(json_str)} chars)")
return json_strSo my entire analysis — parsed, grouped, summarised — becomes a JSON string I can paste into the advisor's Notion page or hand to any tool.
Your methods section is now a single function call that produces a paste-ready string.
I called demographic_summary from Day 14 inside this function. Five weeks of functions, all composing into a pipeline that ends with a shareable JSON output.
json.dumps raises TypeError for values that aren't JSON-serialisable — sets, datetime objects, custom classes. Python dicts, lists, strings, ints, floats, and bools all serialise cleanly. If you add other types to your response dicts, convert them to strings first.
import json
# dict → JSON string
json_str = json.dumps(data, indent=2)
# JSON string → dict
data_back = json.loads(json_str)| Python | JSON |
|---|---|
True / False | true / false |
None | null |
| Single quotes | Double quotes |
Any API, browser, or reporting tool expects JSON — not Python repr.
indent=2Omit for compact single-line output. Use indent=2 for human-readable multi-line output.
demographic_summary produces a nested dict — {"Junior": {"count": 42, "avg_satisfaction": 3.87}}. Your advisor wants to paste it into their notes app, email it, or load it into a reporting tool. How do you hand it over?
load_responses_from_csv from yesterday gets me to the dict. But a Python dict is not something I can paste into an email or store as a file.
json.dumps converts a Python dict to a JSON-formatted string. indent=2 makes it readable — nested structure, two-space indentation, the way every API response and config file looks. json.loads goes the other way, string back to dict:
import json
data = {"Junior": {"count": 42, "avg_satisfaction": 3.87}}
json_str = json.dumps(data, indent=2)
print(json_str)
# {\n "Junior": {\n "count": 42,\n "avg_satisfaction": 3.87\n }\n}Why not just print the dict directly? It already shows the values.
A printed dict uses Python syntax — single quotes, True/False capitalised, no trailing commas. JSON uses double quotes and true/false. They look similar but any JSON parser, API, or reporting tool expects the JSON format, not Python syntax:
def responses_to_json(responses: list) -> str:
"""Serialise the demographic summary to a JSON string."""
summary = demographic_summary(responses, "year")
json_str = json.dumps(summary, indent=2)
print(f"JSON output ({len(json_str)} chars)")
return json_strSo my entire analysis — parsed, grouped, summarised — becomes a JSON string I can paste into the advisor's Notion page or hand to any tool.
Your methods section is now a single function call that produces a paste-ready string.
I called demographic_summary from Day 14 inside this function. Five weeks of functions, all composing into a pipeline that ends with a shareable JSON output.
json.dumps raises TypeError for values that aren't JSON-serialisable — sets, datetime objects, custom classes. Python dicts, lists, strings, ints, floats, and bools all serialise cleanly. If you add other types to your response dicts, convert them to strings first.
import json
# dict → JSON string
json_str = json.dumps(data, indent=2)
# JSON string → dict
data_back = json.loads(json_str)| Python | JSON |
|---|---|
True / False | true / false |
None | null |
| Single quotes | Double quotes |
Any API, browser, or reporting tool expects JSON — not Python repr.
indent=2Omit for compact single-line output. Use indent=2 for human-readable multi-line output.
You need to send her demographic analysis to your thesis advisor as a JSON string — readable, pasteable, and loadable by any tool. Write `responses_to_json(responses)` that calls `demographic_summary` on the responses grouped by 'year', then serialises the result to a JSON string with 2-space indentation.
Tap each step for scaffolded hints.
No blank-editor panic.