[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:
https://docs.python.org/2/library/json.html
or for Python 3:
https://docs.python.org/3/library/json.html
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
Example:
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!'}
--
Steven
More information about the Tutor
mailing list