Python 3home |
Introduction to Python
davidbpython.com
JavaScript Object Notation is a simple "data interchange" format for sending or storing structured data as text.
Fortunately for us, JSON resembles Python in many ways, making it easy to read and understand.
{ "key1": ["a", "b", "c"], "key2": { "innerkey1": 5, "innerkey2": "woah" }, "key3": false, "key4": null }
The json.load() function decodes the contents of a JSON file.
Here's a program to read the structure shown earlier, read from a file that contains it:
import json # we use this module to read JSON
fh = open('sample.json')
mys = json.load(fh) # load from a file, convert into Python container
fh.close()
print((type(mys))) # dict (the outer container of this struct)
print(mys['key2']['innerkey2']) # woah
The json.loads() function decodes the contents of a JSON string.
In this example, we show what we must do if we don't have access to a file, but instead receive the data as a string, as in the case with web request.
import json # we use this module to read JSON
import requests # use 'pip install' to install
response = requests.get('https://davidbpython.com/mystruct.json')
text = response.text # file data as a single string
mys = json.loads(text) # load from a file, convert into Python container
print((type(mys))) # dict (the outer container of this struct)
print((mys['key2']['innerkey2'])) # woah
The requests module allows your Python program to act like a web browser, making web requests (i.e., with a URL) and downloading the response. If you try this program and receive a ModuleNotFoundError, you must run pip install at the command line to install it.
A nested object can be confusing to read.
If we have an multidimensional object that is squished together and hard to read, we can use .dumps() with indent=4
import json
obj = {'a': {'x': 1, 'y': 2, 'z': 3}, 'b': {'x': 1, 'y': 2, 'z': 3}, 'c': {'x': 1, 'y': 2, 'z': 3} }
print((json.dumps(obj, indent=4)))
this prints:
{ "a": { "x": 1, "y": 2, "z": 3 }, "b": { "x": 1, "y": 2, "z": 3 } }
We can use json.dump() write to a JSON file.
Dumping a Python structure to JSON
import json
wfh = open('newfile.json', 'w') # open file for writing
obj = {'a': 1, 'b': 2}
json.dump(obj, wfh)
wfh.close()