📂 Files & JSON

Files & JSON: JSON is like a neutral language a Python program and a Java program both happen to speak — each sets aside its "native tongue" (dict, HashMap) and switches to a sha

JSON is like a neutral language a Python program and a Java program both happen to speak — each sets aside its "native tongue" (dict, HashMap) and switches to a shared format. But there's a real trap here: parse '{"retries": "3", "timeout": 30}' and "retries" arrives as a STRING (because it's quoted in the JSON), while "timeout" arrives as a number (unquoted) — same line, different behavior. Write config["retries"] * config["timeout"] and Python won't raise an error — it'll happily turn "3" * 30 into a string repeated 30 times, and no math ever happens! The same care applies when writing with json.dump(): opening and closing a file with "with open(...) as f:" prevents it from being left HALF-written if something goes wrong mid-write. So if JSON is a "shared language", why are we still fighting type surprises — isn't the format standardised? The format is; JSON's type set is deliberately POOR: string, number, boolean, null, array, object. There is no date type, no decimal-precision guarantee, no integer-versus-float distinction. Every rich type you write into JSON must therefore be re-interpreted on the other side — which is exactly why mapping to a POJO with Jackson in Java makes you write a formatter for LocalDate. In Python, because dict is so flexible, that conversion step gets SKIPPED and the error is deferred to runtime. The QA rule: before asserting a value from an API response, assert its type too — the gap between assert config["retries"] == 3 and assert config["retries"] == "3" is the gap between catching a real contract bug and shipping it.

Java requires Jackson or Gson library. Python's built-in 'json' module is sufficient — just import it. json.dumps() converts Python to JSON string, json.loads() converts JSON string back to Python.

Micro Lab: Python coding practice

Replace the TODO line with the critical line from the expected solution. This is not a real runtime; the goal is to reinforce writing the correct structure in a controlled way.

Which function converts a Python dict to a JSON string?

json.dumps() (dump string) converts Python objects to JSON string. json.loads() (load string) does the reverse. Like JavaScript's JSON.stringify() and JSON.parse().

Which function converts a JSON string to a Python dictionary?

json.loads() (load string) converts JSON data into a Python object. Conversely, json.dump() or json.dumps() convert Python objects into JSON data.

How Does json.dumps() Turn a Python Dict Into Text?

test_result is a Python dict…

test_result is a Python dict — a live DATA structure that LIVES in memory, not yet READY to be sent to a FILE or over the NETWORK.

Calling json.dumps(test_result)…

Calling json.dumps(test_result) (dumps = 'dump STRING') makes Python WALK the dict and CONVERT each key/value into a text string following JSON SYNTAX.

A NESTED list (tags: [...])…