Your load_customers_from_csv gives you a clean customer list. How do you currently share the plan summary with your engineering team or a webhook?
After load_customers_from_csv I can load the data. But sharing it — pasting into Slack, sending to a webhook — means I need a string format. JSON is what everything speaks.
json.dumps(data, indent=2) takes any Python dict or list and turns it into a formatted JSON string. Two lines:
import json
summary = plan_summary(customers)
output = json.dumps(summary, indent=2)
print(output)What's indent=2? Can I just use json.dumps(data) without it?
You can — json.dumps(data) produces a compact one-line string. indent=2 adds pretty-printing with two spaces per level. For a Slack message or reading in a terminal, indent=2 is much more readable. Here's the full function:
def customers_to_json(customers: list) -> str:
import json
summary = plan_summary(customers)
result = json.dumps(summary, indent=2)
print(f"JSON length: {len(result)} chars")
return resultSo the output is a JSON string I can paste directly into a Slack webhook body. The whole plan summary — totals, ARPU, every plan — in one call.
Your Slack bot will thank you. json.dumps is the universal handshake between Python and every other system.
I can now load a Stripe CSV, compute the plan summary, and export it as JSON in three function calls. That's a real data pipeline.
json.loads(s) is the reverse — turns a JSON string back into a Python dict. The round-trip is json.loads(json.dumps(data)) == data. Always true for JSON-serialisable values: strings, numbers, bools, lists, dicts, and null.
json.dumps converts Python data to a JSON string. json.loads reverses it.
import json
json.dumps({"mrr": 14850.0}, indent=2) # pretty string
json.loads('{"mrr": 14850.0}') # back to dict| Python | JSON |
|---|---|
str | string |
int / float | number |
bool | true / false |
None | null |
list | array |
dict | object |
datetime, set, and Decimal are NOT serialisable. Convert to string or float before dumping.
Your load_customers_from_csv gives you a clean customer list. How do you currently share the plan summary with your engineering team or a webhook?
After load_customers_from_csv I can load the data. But sharing it — pasting into Slack, sending to a webhook — means I need a string format. JSON is what everything speaks.
json.dumps(data, indent=2) takes any Python dict or list and turns it into a formatted JSON string. Two lines:
import json
summary = plan_summary(customers)
output = json.dumps(summary, indent=2)
print(output)What's indent=2? Can I just use json.dumps(data) without it?
You can — json.dumps(data) produces a compact one-line string. indent=2 adds pretty-printing with two spaces per level. For a Slack message or reading in a terminal, indent=2 is much more readable. Here's the full function:
def customers_to_json(customers: list) -> str:
import json
summary = plan_summary(customers)
result = json.dumps(summary, indent=2)
print(f"JSON length: {len(result)} chars")
return resultSo the output is a JSON string I can paste directly into a Slack webhook body. The whole plan summary — totals, ARPU, every plan — in one call.
Your Slack bot will thank you. json.dumps is the universal handshake between Python and every other system.
I can now load a Stripe CSV, compute the plan summary, and export it as JSON in three function calls. That's a real data pipeline.
json.loads(s) is the reverse — turns a JSON string back into a Python dict. The round-trip is json.loads(json.dumps(data)) == data. Always true for JSON-serialisable values: strings, numbers, bools, lists, dicts, and null.
json.dumps converts Python data to a JSON string. json.loads reverses it.
import json
json.dumps({"mrr": 14850.0}, indent=2) # pretty string
json.loads('{"mrr": 14850.0}') # back to dict| Python | JSON |
|---|---|
str | string |
int / float | number |
bool | true / false |
None | null |
list | array |
dict | object |
datetime, set, and Decimal are NOT serialisable. Convert to string or float before dumping.
Sam needs to share her weekly plan summary as a JSON string — pasted into Slack or sent to a webhook. Write `customers_to_json(customers)` that calls `plan_summary` internally and returns the summary serialised as a JSON string with indent=2.
Tap each step for scaffolded hints.
No blank-editor panic.