load_clients_from_csv from yesterday gives you a list of client dicts. status_summary from Week 2 gives you a nested dict per status group. Now you need to send that summary to a colleague, store it in a file, or email it as JSON. How do you turn a Python dict into a string?
str() would work — but it gives Python syntax, not JSON. Quotes are different, True and False are capitalised.
Exactly right. json.dumps(data, indent=2) produces valid JSON with proper quoting. json.loads(json_string) reverses it. The round-trip test — dumps then loads — is the way to verify your data survived serialisation without corruption:
import json
summary = {"on track": {"total_hours": 6.5, "total_revenue": 700.0}}
json_str = json.dumps(summary, indent=2)
recovered = json.loads(json_str)
print(recovered == summary) # TrueWhat does indent=2 do? I have seen it in examples but skipping it also works.
Without indent, json.dumps outputs one compressed line — machine-readable but not human-readable. indent=2 adds newlines and two-space indentation per level. Use indent=2 for anything a person will read; omit it for payloads going to an API.
So I can call status_summary on the parsed clients, then clients_to_json to produce a JSON report I can email.
CSV in, JSON out. You have just built your first data pipeline that a non-Python colleague can receive and open in any editor:
import json
def clients_to_json(clients: list) -> str:
summary = status_summary(clients)
json_str = json.dumps(summary, indent=2)
recovered = json.loads(json_str)
print(f"JSON round-trip verified: {recovered == summary}")
return json_strMy monthly review spreadsheet is now a JSON report I generate with one function call.
One caution: json.dumps cannot serialise Python sets, datetime objects, or Decimal. Keep your values as str, int, float, bool, list, dict, or None and the pipeline stays clean.
json.dumps and json.loadsjson.dumps(obj, indent=2) serialises a Python object to a JSON-formatted string:
import json
original = {'name': 'Acme', 'total': 540.0}
serialised = json.dumps(original, indent=2)json.loads(s) deserialises a JSON string back to Python. The two are inverses:
restored = json.loads(serialised)
assert original == restored # TrueSerialise the summary, not raw clients: status_summary(clients) returns aggregated stats — that is what you want to preserve or email. Raw client records can be re-parsed from the CSV source at any time.
load_clients_from_csv from yesterday gives you a list of client dicts. status_summary from Week 2 gives you a nested dict per status group. Now you need to send that summary to a colleague, store it in a file, or email it as JSON. How do you turn a Python dict into a string?
str() would work — but it gives Python syntax, not JSON. Quotes are different, True and False are capitalised.
Exactly right. json.dumps(data, indent=2) produces valid JSON with proper quoting. json.loads(json_string) reverses it. The round-trip test — dumps then loads — is the way to verify your data survived serialisation without corruption:
import json
summary = {"on track": {"total_hours": 6.5, "total_revenue": 700.0}}
json_str = json.dumps(summary, indent=2)
recovered = json.loads(json_str)
print(recovered == summary) # TrueWhat does indent=2 do? I have seen it in examples but skipping it also works.
Without indent, json.dumps outputs one compressed line — machine-readable but not human-readable. indent=2 adds newlines and two-space indentation per level. Use indent=2 for anything a person will read; omit it for payloads going to an API.
So I can call status_summary on the parsed clients, then clients_to_json to produce a JSON report I can email.
CSV in, JSON out. You have just built your first data pipeline that a non-Python colleague can receive and open in any editor:
import json
def clients_to_json(clients: list) -> str:
summary = status_summary(clients)
json_str = json.dumps(summary, indent=2)
recovered = json.loads(json_str)
print(f"JSON round-trip verified: {recovered == summary}")
return json_strMy monthly review spreadsheet is now a JSON report I generate with one function call.
One caution: json.dumps cannot serialise Python sets, datetime objects, or Decimal. Keep your values as str, int, float, bool, list, dict, or None and the pipeline stays clean.
json.dumps and json.loadsjson.dumps(obj, indent=2) serialises a Python object to a JSON-formatted string:
import json
original = {'name': 'Acme', 'total': 540.0}
serialised = json.dumps(original, indent=2)json.loads(s) deserialises a JSON string back to Python. The two are inverses:
restored = json.loads(serialised)
assert original == restored # TrueSerialise the summary, not raw clients: status_summary(clients) returns aggregated stats — that is what you want to preserve or email. Raw client records can be re-parsed from the CSV source at any time.
Mira needs to email the monthly status summary as a JSON report that any colleague can open. Write `clients_to_json(clients)` that calls `status_summary(clients)` to get the nested summary dict, serialises it with `json.dumps(indent=2)`, and returns the JSON string. Use `json.loads` to verify the round-trip before returning.
Tap each step for scaffolded hints.
No blank-editor panic.