load_respondents_from_csv from Day 20 gives you the respondent list. treatment_summary from Day 14 gives you the stats dict. The reviewer wants the full summary table as a supplementary file. How do you turn a Python dict into something shareable?
I could print the dict, but that's ugly Python repr syntax. I need proper JSON for a supplementary materials doc.
json.dumps serialises any Python dict to a JSON string. indent=2 adds indentation for human readability. json.loads round-trips it back:
import json
summary = {"control": {"n": 87, "mean_outcome": 4.72}}
json_str = json.dumps(summary, indent=2)
reloaded = json.loads(json_str) # back to a dictWhy would I reload JSON I just serialised? When would that round-trip matter?
The round-trip test is a reproducibility check. If json.loads(json.dumps(data)) == data, your summary is fully serialisable — no Python-only objects like set or datetime snuck in. It's the same rigour as checking that a function reruns cleanly on wave 4 data.
So I pipe treatment_summary into json.dumps and the result is a supplementary JSON file I can attach to the submission?
A supplementary file generated by the exact same pipeline that computed the stats. Reviewer 2 can check that the JSON numbers match the methods section because they come from the same function:
import json
def respondents_to_json(respondents: list) -> str:
summary = treatment_summary(respondents)
json_str = json.dumps(summary, indent=2)
print(f"JSON length: {len(json_str)} chars")
return json_strThe pipeline is complete. CSV in, JSON out. Five days from formatted labels to a publishable supplementary table.
One limit: json.dumps only handles str, int, float, bool, None, list, dict. If treatment_summary accidentally returns a tuple as a value instead of a list, json.dumps serialises it as a JSON array — which is correct — but json.loads returns a list, not a tuple. Round-trip equality holds, but type parity doesn't. Document what your pipeline returns.
json.dumps(obj) serialises a Python object to a JSON string. json.loads(s) deserialises a JSON string back to Python.
import json
obj = {"n": 87, "mean": 4.72}
s = json.dumps(obj, indent=2) # pretty-printed JSON string
back = json.loads(s) # back to a Python dict| Python | JSON |
|---|---|
dict | object {} |
list, tuple | array [] |
str | string "" |
int, float | number |
bool | true/false |
None | null |
indent=None → compact one-line string. indent=2 → human-readable with 2-space indentation. Use indent=2 for supplementary files.
load_respondents_from_csv from Day 20 gives you the respondent list. treatment_summary from Day 14 gives you the stats dict. The reviewer wants the full summary table as a supplementary file. How do you turn a Python dict into something shareable?
I could print the dict, but that's ugly Python repr syntax. I need proper JSON for a supplementary materials doc.
json.dumps serialises any Python dict to a JSON string. indent=2 adds indentation for human readability. json.loads round-trips it back:
import json
summary = {"control": {"n": 87, "mean_outcome": 4.72}}
json_str = json.dumps(summary, indent=2)
reloaded = json.loads(json_str) # back to a dictWhy would I reload JSON I just serialised? When would that round-trip matter?
The round-trip test is a reproducibility check. If json.loads(json.dumps(data)) == data, your summary is fully serialisable — no Python-only objects like set or datetime snuck in. It's the same rigour as checking that a function reruns cleanly on wave 4 data.
So I pipe treatment_summary into json.dumps and the result is a supplementary JSON file I can attach to the submission?
A supplementary file generated by the exact same pipeline that computed the stats. Reviewer 2 can check that the JSON numbers match the methods section because they come from the same function:
import json
def respondents_to_json(respondents: list) -> str:
summary = treatment_summary(respondents)
json_str = json.dumps(summary, indent=2)
print(f"JSON length: {len(json_str)} chars")
return json_strThe pipeline is complete. CSV in, JSON out. Five days from formatted labels to a publishable supplementary table.
One limit: json.dumps only handles str, int, float, bool, None, list, dict. If treatment_summary accidentally returns a tuple as a value instead of a list, json.dumps serialises it as a JSON array — which is correct — but json.loads returns a list, not a tuple. Round-trip equality holds, but type parity doesn't. Document what your pipeline returns.
json.dumps(obj) serialises a Python object to a JSON string. json.loads(s) deserialises a JSON string back to Python.
import json
obj = {"n": 87, "mean": 4.72}
s = json.dumps(obj, indent=2) # pretty-printed JSON string
back = json.loads(s) # back to a Python dict| Python | JSON |
|---|---|
dict | object {} |
list, tuple | array [] |
str | string "" |
int, float | number |
bool | true/false |
None | null |
indent=None → compact one-line string. indent=2 → human-readable with 2-space indentation. Use indent=2 for supplementary files.
Mei needs to serialise the treatment summary dict to JSON for a supplementary materials file. Write `respondents_to_json(respondents)` that calls `treatment_summary` to get the stats dict and returns it as a `json.dumps` string with `indent=2`.
Tap each step for scaffolded hints.
No blank-editor panic.