[Tutor] Parsing JSON with Python

Steven D'Aprano steve at pearwood.info
Fri Dec 12 00:51:56 CET 2014

On Thu, Dec 11, 2014 at 02:25:53PM -0600, Galen Seilis wrote:

> I would appreciate assitance understanding why this doesn't work, and how I
> can get up-and-running with inputing JSON code into Python.

Did you try reading the Fine Manual? It has many examples!

For Python 2:

or for Python 3:

The four functions you are likely to care about are:

json.load   # read JSON input from a file, return objects
json.loads  # read JSON input from a string, return objects
json.dump   # dump JSON output to a file
json.dumps  # dump JSON output to a string


py> import json
py> data = {23: None, 42: "Hello World!"}
py> s = json.dumps(data)
py> print(s)
{"42": "Hello World!", "23": null}
py> obj = json.loads(s)
py> print(obj)
{'23': None, '42': 'Hello World!'}


More information about the Tutor mailing list