Talking to the Outside World
JSON looks almost exactly like a Python dictionary, right up until you notice the differences. Convert one into the other in both directions, and find the two places the conversion is not quite what you expected.
Worth reading first: Looking Things Up Instead of Counting Along
JSON looks almost exactly like a Python dictionary, right up until you notice the differences. Convert one into the other in both directions, and find the two places the conversion is not quite what you expected.
The format everything on the internet already speaks
JSON — JavaScript Object Notation, despite the name — is the text format almost every web API sends and receives data in. It is not Python, and not JavaScript; it is a shared, language-neutral shape that both happen to read easily.
{
"name": "Ada",
"age": 36,
"active": true,
"manager": null
}json.loads(text) turns this string into a real dict — and along the way, true becomes True, and null becomes None.
Loading JSON turns it into ordinary Python values
$ >>> import json$ >>> text = '{"name": "Ada", "age": 36}'$ >>> record = json.loads(text)$ >>> record$ {'name': 'Ada', 'age': 36}$ >>> record["age"]$ 36json.loads(text) parses a JSON string into an ordinary Python dict — after this line, it behaves exactly like any dictionary built by hand, with no trace of where it came from. json.dumps(record) does the reverse, turning a dict back into a JSON string.
Where the two formats quietly disagree
JSON has no distinct type for a whole number versus a decimal the way some languages do, and more importantly: JSON spells its boolean and null values true, false, and null, lowercase — Python spells the same values True, False, and None, capitalised. The json module translates between them automatically in both directions, but writing raw JSON by hand with True instead of true is a common, easy-to-miss source of a parse error.
Key takeaways
- JSON is a text format, not Python or JavaScript specifically — it's the shared shape most web APIs speak.
- json.loads(text) parses a JSON string into an ordinary Python dict; json.dumps(record) reverses it.
- JSON spells its booleans and null lowercase (true, false, null); Python spells the same values capitalised (True, False, None).
- A JSONDecodeError from hand-written JSON is often just a capitalised True/None, or a trailing comma, sitting where JSON doesn't allow one.